home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / TestParts / DragText / DragText.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  210.0 KB  |  7,270 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DragText.cpp
  3.  
  4.     Contains:    DragText class definition.
  5.  
  6.     Owned by:    Craig Carper
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <6>      12/17/96    CC        1612520: Call SetDroppable in InitPart and
  13.                                     InitPartFromStorage.
  14.          <5>     7/16/96    EL        1295068: Do not externalize unless dirty,
  15.                                     Do not remove hfs info unless dirty.
  16.          <4>     6/20/96    JP        1323103: made some logging optional
  17.          <3>     5/24/96    jpa        1246074: SOM_CATCH --> SOM_TRY & CATCH -->
  18.                                     CATCH_ALL
  19.  
  20.     To Do:
  21.         •    Restore allocation of ODCPlusSemanticInterface and add SIHelper object
  22.         •    AdjustMenus() is being called when the part is in an inactive window;
  23.             is this right?  Our fHasFocus flag then seems to be true (not investigated).
  24.         •    $$$$$ fDocument->frameIsActive is redundant with fHasFoci and fNeedsFoci.
  25.             I suspect fDocument was introduced to support multiple display frames;
  26.             perhaps it makes more sense to move fHasFoci and fNeedsFoci there, and to
  27.             write fDocument as part info in the display frame.
  28.     
  29.     In Progress:
  30.  
  31. */
  32.  
  33.  
  34. #ifndef _ALTPOINT_
  35. #include "AltPoint.h"            // Use C++ savvy ODPoint and ODRect
  36. #endif
  37.  
  38. #define SOM_Module_dragtext_Source
  39. #define VARIABLE_MACROS
  40. #include <DragText.xih>
  41.  
  42. #ifndef _DRAGDEF_
  43. #include <DragDef.h>
  44. #endif
  45.  
  46. #ifndef _DRAGCONS_
  47. #include <DragCons.h>
  48. #endif
  49.  
  50. #ifndef _DRAGTXPV_
  51. #include <DragTxPv.h>
  52. #endif
  53.  
  54. #ifndef _PLFMDEF_
  55. #include "PlfmDef.h"
  56. #endif
  57.  
  58. #ifndef _SIHELPER_
  59. #include "SIHelper.h"
  60. #endif
  61.  
  62. #ifndef __QUICKDRAW__
  63. #include <Quickdraw.h>
  64. #endif
  65.  
  66. #ifndef __SCRIPT__
  67. #include <Script.h>
  68. #endif
  69.  
  70. #ifndef __STRING__
  71. #include <String.h>
  72. #endif
  73.  
  74. #ifndef __OSUTILS__
  75. #include <OSUtils.h>    // SysBeep, GetDateTime, etc.
  76. #endif
  77.  
  78. #ifndef __OSEVENTS__
  79. #include <OSEvents.h>
  80. #endif
  81.  
  82. #ifndef __TOOLUTILS__
  83. #include <ToolUtils.h>    // HiWord etc.
  84. #endif
  85.  
  86. #ifndef __SCRAP__
  87. #include <Scrap.h>
  88. #endif
  89.  
  90. #ifndef __LOWMEM__
  91. #include <LowMem.h>
  92. #endif
  93.  
  94. #ifndef __FILES__
  95. #include <Files.h>
  96. #endif
  97.  
  98. #ifdef __SC__
  99.  
  100. #ifndef __LANGUAGE__
  101. #include <Language.h>
  102. #endif
  103.  
  104. #endif // __SC__
  105.  
  106. #ifndef SOM_Module_OpenDoc_StdProps_defined
  107. #include <StdProps.xh>
  108. #endif
  109.  
  110. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  111. #include <StdTypes.xh>
  112. #endif
  113.  
  114. #ifndef SOM_ODStorageUnit_xh
  115. #include <StorageU.xh>
  116. #endif
  117.  
  118. #ifndef SOM_ODDraft_xh
  119. #include <Draft.xh>
  120. #endif
  121.  
  122. #ifndef SOM_ODDocument_xh
  123. #include <Document.xh>
  124. #endif
  125.  
  126. #ifndef SOM_ODContainer_xh
  127. #include <ODCtr.xh>
  128. #endif
  129.  
  130. #ifndef SOM_ODSession_xh
  131. #include <ODSessn.xh>
  132. #endif
  133.  
  134. #ifndef SOM_ODWindowState_xh
  135. #include <WinStat.xh>
  136. #endif
  137.  
  138. #ifndef SOM_ODArbitrator_xh
  139. #include <Arbitrat.xh>
  140. #endif
  141.  
  142. #ifndef SOM_ODDispatcher_xh
  143. #include <Disptch.xh>
  144. #endif
  145.  
  146. #ifndef SOM_ODFrame_xh
  147. #include <Frame.xh>
  148. #endif
  149.  
  150. #ifndef SOM_ODFacet_xh
  151. #include <Facet.xh>
  152. #endif
  153.  
  154. #ifndef SOM_ODFrameFacetIterator_xh
  155. #include <FrFaItr.xh>
  156. #endif
  157.  
  158. #ifndef SOM_ODCanvas_xh
  159. #include <Canvas.xh>
  160. #endif
  161.  
  162. #ifndef SOM_ODDraft_xh
  163. #include <Draft.xh>
  164. #endif
  165.  
  166. #ifndef SOM_ODShape_xh
  167. #include <Shape.xh>
  168. #endif
  169.  
  170. #ifndef SOM_ODTransform_xh
  171. #include <Trnsform.xh>
  172. #endif
  173.  
  174. #ifndef SOM_ODWindow_xh
  175. #include <Window.xh>
  176. #endif
  177.  
  178. #ifndef SOM_ODMenuBar_xh
  179. #include <MenuBar.xh>
  180. #endif
  181.  
  182. #ifndef _EXCEPT_
  183. #include "Except.h"
  184. #endif
  185.  
  186. #ifndef SOM_ODDragAndDrop_xh
  187. #include <DragDrp.xh>
  188. #endif
  189.  
  190. #ifndef SOM_ODDragItemIterator_xh
  191. #include <DgItmIt.xh>
  192. #endif
  193.  
  194. #ifndef SOM_ODTranslation_xh
  195. #include <Translt.xh>
  196. #endif
  197.  
  198. #ifndef SOM_ODStorageUnitView_xh
  199. #include <SUView.xh>
  200. #endif
  201.  
  202. #ifndef SOM_Module_OpenDoc_Commands_defined
  203. #include <CmdDefs.xh>
  204. #endif
  205.  
  206. #ifndef SOM_ODUndo_xh
  207. #include <Undo.xh>
  208. #endif
  209.  
  210. #ifndef SOM_ODFocusSet_xh
  211. #include <FocusSet.xh>
  212. #endif
  213.  
  214. #ifndef SOM_Module_OpenDoc_Foci_defined
  215. #include <Foci.xh>
  216. #endif
  217.  
  218. #ifndef _ODUTILS_
  219. #include <ODUtils.h>
  220. #endif
  221.  
  222. #ifndef _INFOUTIL_
  223. #include <InfoUtil.h>
  224. #endif
  225.  
  226. #ifndef _DOCUTILS_
  227. #include <DocUtils.h>
  228. #endif
  229.  
  230. #ifndef _USERSRCM_
  231. #include "UseRsrcM.h"
  232. #endif
  233.  
  234. #ifndef _ORDCOLL_
  235. #include "OrdColl.h"
  236. #endif
  237.  
  238. #ifndef SOM_ODClipboard_xh
  239. #include <Clipbd.xh>
  240. #endif
  241.  
  242. #ifndef SOM_ODLink_xh
  243. #include <Link.xh>
  244. #endif
  245.  
  246. #ifndef SOM_ODLinkSource_xh
  247. #include <LinkSrc.xh>
  248. #endif
  249.  
  250. #ifndef SOM_ODLinkSpec_xh
  251. #include <LinkSpec.xh>
  252. #endif
  253.  
  254. #ifndef _ODMEMORY_
  255. #include "ODMemory.h"
  256. #endif
  257.  
  258. #ifndef _ISOSTR_
  259. #include "ISOStr.h"
  260. #endif
  261.  
  262. #ifndef SOM_ODNameSpaceManager_xh
  263. #include <NmSpcMg.xh>
  264. #endif
  265.  
  266. #ifndef SOM_ODValueNameSpace_xh
  267. #include <ValueNS.xh>
  268. #endif
  269.  
  270. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  271. #include <StdDefs.xh>    //jpa
  272. #endif
  273.  
  274. #ifndef _FOCUSLIB_
  275. #include "FocusLib.h"    // for drawing/printing
  276. #endif
  277.  
  278. #ifndef __GXMATH__
  279. #include <GXMath.h>
  280. #endif
  281.  
  282. #ifndef __QDOFFSCREEN__
  283. #include "QDOffscreen.h"    // for GWorldPtr, StScrpHandle
  284. #endif
  285.  
  286. #ifndef __STDLIB__
  287. #include "stdlib.h"    // for labs
  288. #endif
  289.  
  290. #ifndef _PASCLSTR_
  291. #include "PasclStr.h"
  292. #endif
  293.  
  294. #ifndef _ITEXT_
  295. #include <IText.h>
  296. #endif
  297.  
  298. #ifndef __PROCESSES__
  299. #include <Processes.h>
  300. #endif
  301.  
  302. #ifndef __DIALOGS__
  303. #include "Dialogs.h"
  304. #endif
  305.  
  306. #ifndef _BARRAY_
  307. #include <BArray.h>
  308. #endif
  309.  
  310. #ifndef _STORUTIL_
  311. #include <StorUtil.h>
  312. #endif
  313.  
  314. #ifndef SOM_ODObjectNameSpace_xh
  315. #include "ObjectNS.xh"
  316. #endif
  317.  
  318. #ifndef _WINUTILS_
  319. #include "WinUtils.h"
  320. #endif
  321.  
  322. #ifndef _ODDEBUG_
  323. #include <ODDebug.h>
  324. #endif
  325.  
  326. #ifndef SOM_ODFocusSetIterator_xh
  327. #include <FocusItr.xh>
  328. #endif
  329.  
  330. #ifndef _TEMPOBJ_
  331. #include <TempObj.h>
  332. #endif
  333.  
  334. #ifndef _TEMPITER_
  335. #include <TempIter.h>
  336. #endif
  337.  
  338. #ifndef SOM_AppleTestDrag_SemtIntf_xh
  339. #include <DragSI.xh>
  340. #endif
  341.  
  342. #ifndef SOM_Module_OpenDoc_StandardExtensions_defined
  343. #include <StdExts.xh>
  344. #endif
  345.  
  346. #pragma segment DragText
  347.  
  348.  
  349. //==============================================================================
  350. // Constants
  351. //==============================================================================
  352.  
  353. #define DRAGTEXT_SUPPORTS_SCRIPTING 1
  354.  
  355. #define ODDebugCursor    0    // debug cursor location in MouseEnter etc.
  356.  
  357. // Define this symbol to enable support of kODKindTestDrag and allow linking
  358. #define SUPPORT_TESTDRAG_KIND
  359.  
  360. #define SHOW_LINK_BORDERS
  361. #undef DEBUG_LINK_BORDERS
  362.  
  363. // Allow links to testdrag and styled text content
  364. #undef LINK_TO_TEXT_ONLY
  365.  
  366. // Define to test transmission of content frame property to link destination
  367. #undef BOGUS_CONTENT_FRAME
  368.  
  369. #undef SHOW_SCROLL_BARS
  370. #undef SHOW_TEXT_MENU
  371.  
  372. #define SHOW_CLIPBOARD_PASTE_AS_DIALOG
  373. #define SHOW_DROP_PASTE_AS_DIALOG
  374.  
  375. #define SSREMOVEWORKAROUND
  376.  
  377. // The following #define is used for trying out promise resolution.
  378. // It creates a storage unit during promise resolution. This would cause
  379. // CMDraft to throw if CMDraft is not implemented correctly.
  380. #undef TRYOUT_PROMISE
  381.  
  382. // The following #define is used to test creating a TEXT file by dragging
  383. // to the Finder.
  384. #undef TRYOUT_PROMISE_FILE
  385.  
  386. #define    MaxDocumentCount        8
  387.  
  388. #define kDTIdleFrequency    16
  389.  
  390. #define    InitialH                16
  391. #define    InitialV                42
  392.  
  393. const char kRightArrow    = 0x1D;
  394. const char kLeftArrow    = 0x1C;
  395. const char kUpArrow        = 0x1E;
  396. const char kDownArrow    = 0x1F;
  397.  
  398. //==============================================================================
  399. // Local Structs
  400. //==============================================================================
  401.  
  402. struct UndoInfo
  403. {
  404.     ODFrame*        displayFrame;
  405.     ODCommandID        command;
  406.     ODULong            start;
  407.     ODULong            end;
  408.     ODPtr            text;
  409.     ODHandle        style;
  410. };
  411.  
  412.  
  413. //==============================================================================
  414. // DragText Command Numbers
  415. //==============================================================================
  416.  
  417. #define cDTSampleOne    24001
  418. #define cDTSampleTwo    24002
  419. #define cDTTyping        29999
  420. #define cDTStyling        29998
  421.  
  422. #define kUndoIt            1
  423. #define kRedoIt            2
  424.  
  425. //==============================================================================
  426. // Local function declarations
  427. //==============================================================================
  428.  
  429. static void ShowAlert(Environment *ev,
  430.         ODSShort    alertID,
  431.         ODFrame*    frame);
  432.  
  433. static void ShowErrorAlert(Environment *ev,
  434.         Str255        message,
  435.         ODError        error,
  436.         ODFrame*    frame);
  437.  
  438. static void ShowInfoAlert(Environment *ev,
  439.         Str255        message,
  440.         ODFrame*    frame);
  441.  
  442. //==============================================================================
  443. // Local functions
  444. //==============================================================================
  445.  
  446. static void SetTextFont(TEHandle theTE, ODSShort fontNum)
  447. {
  448.     TextStyle *newStyle = (TextStyle *) ODNewPtr(sizeof(TextStyle), kDefaultHeapID);
  449.     
  450.     (*newStyle).tsFont = fontNum;    
  451.     
  452.     TESetStyle(doFont, newStyle, kODTrue, theTE);
  453.     
  454.     ODDisposePtr((Ptr)newStyle);
  455. }
  456.  
  457. static void SetTextSize(TEHandle theTE, ODSShort fontSize)
  458. {
  459.     TextStyle *newStyle = (TextStyle *) ODNewPtr(sizeof(TextStyle), kDefaultHeapID);
  460.  
  461.     (*newStyle).tsSize = fontSize;    
  462.     
  463.     TESetStyle(doSize, newStyle, kODTrue, theTE);
  464.     
  465.     ODDisposePtr((Ptr)newStyle);
  466. }
  467.  
  468. static void ShowAlert(Environment *ev,
  469.         ODSShort    alertID,
  470.         ODFrame*    frame)
  471. {
  472.     ODSession* session = frame->GetStorageUnit(ev)->GetSession(ev);
  473.     ODArbitrator* arbitrator = session->GetArbitrator(ev);
  474.  
  475.     // $$$$$ Should use notification manager
  476.     if ( !IsFrontProcess() )
  477.     {
  478.         SysBeep(2);
  479.         return;
  480.     }
  481.  
  482.     ODTypeToken modalFocus = session->Tokenize(ev, kODModalFocus);
  483.  
  484.     ODFrame* currentOwner = arbitrator->AcquireFocusOwner(ev, modalFocus);
  485.     if ( (currentOwner == frame) || arbitrator->RequestFocus(ev, modalFocus, frame) )
  486.     {
  487.         ODSLong savedRefNum;
  488.         BeginUsingLibraryResources(savedRefNum);
  489.         SetCursor(&(ODQDGlobals.arrow));
  490.         Alert(alertID, nil);
  491.         ResetAlertStage();
  492.         EndUsingLibraryResources(savedRefNum);
  493.         if ( (currentOwner != frame) )
  494.             arbitrator->TransferFocus(ev, modalFocus, frame, currentOwner);
  495.     }
  496.     else
  497.     {
  498.         SysBeep(2);
  499.     }
  500.     ODReleaseObject(ev, currentOwner);
  501. }
  502.  
  503. static void ShowErrorAlert(Environment *ev,
  504.         Str255        message,
  505.         ODError        error,
  506.         ODFrame*    frame)
  507. {
  508.         Str255 numberString;
  509.         NumToString(error, numberString);
  510.         ParamText(message, numberString, nil, nil);
  511.         ShowAlert(ev, kDragText_ErrorAlertID, frame);
  512. }
  513.  
  514. static void ShowInfoAlert(Environment *ev,
  515.         Str255        message,
  516.         ODFrame*    frame)
  517. {
  518.         ParamText(message, nil, nil, nil);
  519.         ShowAlert(ev, kDragText_InfoAlertID, frame);
  520. }
  521.  
  522. static void WriteValueType(Environment *ev,
  523.                 ODStorageUnit* su,
  524.                 ODType prop,
  525.                 ODValueType kind,
  526.                 TEHandle theTEHandle,
  527.                 ODBoolean selectionOnly)
  528. {
  529.     Handle        textHandle;
  530.     char        textState;
  531.     
  532.     ODSShort    selStart    = (**theTEHandle).selStart;
  533.     ODSShort    selEnd        = (**theTEHandle).selEnd;
  534.  
  535.     ODULong        textStart;
  536.     ODULong        textSize;
  537.     if ( selectionOnly )
  538.     {
  539.         textStart = selStart;
  540.         textSize = (selEnd < selStart ? 0 : (ODULong) (selEnd - selStart));
  541.     }
  542.     else
  543.     {
  544.         textStart = 0;
  545.         textSize = (**theTEHandle).teLength;
  546.     }
  547.  
  548.     textHandle = (**theTEHandle).hText;
  549.     textState = HGetState(textHandle);
  550.     ODLockHandle((ODHandle) textHandle);
  551.  
  552.     if ( IsKind(kind, kODAppleTEXT) )
  553.     {
  554.         SUFocusAndSetValue(ev, su, prop, kODAppleTEXT, (ODValue) ((*(char **)(textHandle)) + textStart), textSize);
  555.     }
  556.     else
  557.     {
  558.         if ( !selectionOnly )
  559.             TESetSelect(0, 32767, theTEHandle);
  560.  
  561.         StScrpHandle stylHandle = TEGetStyleScrapHandle(theTEHandle);
  562.         ODULong stylSize = ODGetHandleSize((ODHandle) stylHandle);
  563.         ODLockHandle((ODHandle) stylHandle);
  564.     
  565.         if ( IsKind(kind, kODApplestyl) )
  566.         {
  567.             if ( textSize > 0 )
  568.                 SUFocusAndSetValue(ev, su, prop, kODApplestyl, (ODValue) *stylHandle, stylSize);
  569.             else
  570.                 SUFocusAndSetValue(ev, su, prop, kODApplestyl, (ODValue) kODNULL, 0);
  571.         }
  572.         else if ( IsKind(kind, kODApplestxt) )
  573.         {
  574.             ODULong oldSize = 0;
  575.             ODULong newSize = stylSize + textSize;
  576.             SUForceFocus(ev, su, prop, kODApplestxt);
  577.             oldSize = su->GetSize(ev);
  578.  
  579.             su->SetOffset(ev, 0);
  580.             StorageUnitSetValue(su, ev, stylSize, (ODValue) *stylHandle);    
  581.             StorageUnitSetValue(su, ev, textSize, (ODValue) ((*(char **)(textHandle)) + textStart));    
  582.             if ( oldSize > newSize )
  583.                 su->DeleteValue(ev, oldSize - newSize);
  584.         }
  585.     
  586.         ODUnlockHandle((ODHandle) stylHandle);
  587.         ODDisposeHandle((ODHandle) stylHandle);
  588.  
  589.         if ( !selectionOnly )
  590.             TESetSelect(selStart, selEnd, theTEHandle);
  591.     }
  592.     HSetState(textHandle, textState);
  593. }
  594.  
  595. ODStatic void ReadStyledText(Environment *ev,
  596.                 ODStorageUnit*    su,
  597.                 Handle*            textHandle,
  598.                 StScrpHandle*    stylHandle)
  599. {
  600.     *textHandle = kODNULL;
  601.     *stylHandle = kODNULL;
  602.  
  603.     ODULong        size = su->GetSize(ev);
  604.     ODHandle    tempHandle;
  605.     ODValue        value;
  606.  
  607.     // Compute the size of the style information
  608.     short stylSize;
  609.     StorageUnitGetValue(su, ev, sizeof(short), (ODValue) &stylSize);
  610.     stylSize = (stylSize * sizeof(ScrpSTElement)) + sizeof(short);
  611.     if ( stylSize > sizeof(short) )
  612.     {
  613.         // Read the entire style record, including the first two bytes.
  614.         su->SetOffset(ev, 0);
  615.         
  616.         tempHandle = ODNewHandle(stylSize);
  617.         value = ODLockHandle(tempHandle);
  618.         StorageUnitGetValue(su, ev, stylSize, value);
  619.         ODUnlockHandle(tempHandle);
  620.         
  621.         *stylHandle = (StScrpHandle) tempHandle;
  622.     }
  623.  
  624.     // read in TEXT
  625.     if ( size > stylSize )
  626.     {
  627.         ODULong textSize = size - stylSize;
  628.         
  629.         tempHandle = ODNewHandle(textSize);
  630.         value = ODLockHandle(tempHandle);
  631.         StorageUnitGetValue(su, ev, textSize, value);
  632.         ODUnlockHandle(tempHandle);
  633.         
  634.         *textHandle = (Handle) tempHandle;
  635.     }
  636. }
  637.  
  638. ODStatic ODBoolean myValueNameSpaceGetODName(ODValueNameSpace* ns, 
  639.                                  Environment* ev,
  640.                                    ODISOStr key,
  641.                                    ODName** value)
  642. {
  643.     ODByteArray ba;
  644.     
  645.     if (ns->GetEntry(ev, key, &ba))
  646.     {
  647.         ODTradITextData* theIName = (ODTradITextData*)ODNewPtrClear(ba._length, kDefaultHeapID);
  648.         ODBlockMove(ba._buffer, theIName, ba._length);
  649.         ODDisposePtr( ba._buffer );
  650.         ODULong textLen = ba._length - sizeof(ODTradITextDataHeader);
  651.         StringPtr thePString = (StringPtr)ODNewPtrClear(textLen+1);
  652.         thePString[0] = textLen;
  653.         ODBlockMove((ODPtr)theIName->theText, (ODPtr)&thePString[1], textLen );
  654.         *value = CreateITextPString(theIName->theScriptCode, 
  655.                              theIName->theLangCode, 
  656.                              thePString);
  657.         ODDisposePtr( theIName );
  658.         ODDisposePtr( thePString );
  659.         return kODTrue;
  660.     }
  661.     else
  662.     {
  663.         *value = kODNULL;
  664.         return kODFalse;
  665.     }
  666. }
  667.  
  668. //==============================================================================
  669. // DragText
  670. //==============================================================================
  671.  
  672. //-------------------------------------------------------------------------
  673. // DragText: Externalize (Override)
  674. //-------------------------------------------------------------------------
  675.  
  676. SOM_Scope void  SOMLINK AppleTestDrag_DragTextExternalize(AppleTestDrag_DragText *somSelf, Environment *ev)
  677. {
  678.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  679.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","Externalize");
  680.  
  681.     SOM_TRY
  682.  
  683.     parent_Externalize(somSelf,ev);
  684.     
  685.     if (_fDirty) {
  686.     
  687.         ODStorageUnit*        partSU = somSelf->GetStorageUnit(ev);
  688.  
  689. #ifdef SUPPORT_TESTDRAG_KIND
  690.         ODULong             suOffset;
  691.         ODStorageUnitRef    aSURef;
  692.         ODFrame*            aFrame;
  693. #endif
  694.  
  695. #ifdef SUPPORT_TESTDRAG_KIND
  696.         // fTestDragSU is null if internalized from a plain text part
  697.         if ( _fTestDragSU == kODNULL )
  698.             _fTestDragSU = _fDraft->CreateStorageUnit(ev);
  699. #else
  700.         if ( partSU->Exists(ev, kODPropContents, kODKindTestDrag, 0) )
  701.         {
  702.             partSU->Focus(ev, kODPropContents, kODPosUndefined, kODKindTestDrag, 0, kODPosUndefined);        
  703.             partSU->Remove(ev);
  704.         }
  705. #endif
  706.  
  707.         if ( _fDocument != kODNULL )
  708.         {
  709.             // Write the text and style values to the content property
  710.             WriteValueType(ev, partSU, kODPropContents, kODApplestxt, _fDocument->theTE, kODFalse);
  711.             WriteValueType(ev, partSU, kODPropContents, kODAppleTEXT, _fDocument->theTE, kODFalse);
  712.  
  713. #ifdef SUPPORT_TESTDRAG_KIND
  714.             // Write the text and styl values to the TestDrag storage unit
  715.             WriteValueType(ev, _fTestDragSU, kDragTextPropText, kODAppleTEXT, _fDocument->theTE, kODFalse);
  716.             WriteValueType(ev, _fTestDragSU, kDragTextPropStyl, kODApplestyl, _fDocument->theTE, kODFalse);
  717. #endif
  718.         }
  719.         
  720. #ifdef SUPPORT_TESTDRAG_KIND
  721.         //
  722.         // Write out the display frame
  723.         //
  724.         OrderedCollectionIterator aIter(_fDisplayFrames);
  725.         SUForceFocus(ev, _fTestDragSU, kODPropDisplayFrames, kODWeakStorageUnitRefs);
  726.         ODULong oldValueSize = _fTestDragSU->GetSize(ev);
  727.         suOffset = 0;
  728.         for (aFrame = (ODFrame*)aIter.First(); aIter.IsNotComplete();
  729.                                                 aFrame = (ODFrame*)aIter.Next(),
  730.                                                 suOffset+=sizeof(ODStorageUnitRef))
  731.         {
  732.             _fTestDragSU->GetWeakStorageUnitRef(ev, aFrame->GetStorageUnit(ev)->GetID(ev), aSURef);
  733.             _fTestDragSU->SetOffset(ev, suOffset);
  734.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODStorageUnitRef), aSURef);
  735.         }
  736.         if (oldValueSize > suOffset)
  737.             _fTestDragSU->DeleteValue(ev, oldValueSize-suOffset);
  738.         
  739.         //
  740.         // Write out the source and destination links
  741.         //
  742.         if (_fSrcLink.linkSource == (ODLinkSource*) kODNULL)
  743.         {
  744.             SURemoveProperty(ev, _fTestDragSU, kDragTextPropSourceLink);
  745.         }
  746.         else
  747.         {
  748.             SUForceFocus(ev, _fTestDragSU, kDragTextPropSourceLink, kDragTextValue);
  749.             _fTestDragSU->GetStrongStorageUnitRef(ev, _fSrcLink.linkSource->GetStorageUnit(ev)->GetID(ev), aSURef);
  750.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODStorageUnitRef),aSURef);
  751.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fSrcLink.start);
  752.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fSrcLink.end);
  753.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODULong), (ODValue) &_fSrcLink.change);
  754.         }
  755.  
  756.         if (_fDstLink.link == (ODLink*) kODNULL)
  757.         {
  758.             SURemoveProperty(ev, _fTestDragSU, kDragTextPropDestLink);
  759.         }
  760.         else 
  761.         {
  762.             SUForceFocus(ev, _fTestDragSU, kDragTextPropDestLink, kDragTextValue);
  763.             _fTestDragSU->GetStrongStorageUnitRef(ev, _fDstLink.link->GetStorageUnit(ev)->GetID(ev), aSURef);
  764.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODStorageUnitRef), aSURef);
  765.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fDstLink.start);
  766.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fDstLink.end);
  767.  
  768.             ODType kind = _fDstLink.info.kind;
  769.             _fDstLink.info.kind = (ODType) ODISOStrLength(kind) + 1;
  770.             StorageUnitSetValue(_fTestDragSU, ev, sizeof(ODLinkInfo), (ODValue) &_fDstLink.info);
  771.             StorageUnitSetValue(_fTestDragSU, ev, (ODULong) _fDstLink.info.kind, (ODValue) kind);
  772.             _fDstLink.info.kind = kind;
  773.         }
  774.  
  775.         SUForceFocus(ev, partSU, kODPropContents, kODKindTestDrag);
  776.         partSU->GetStrongStorageUnitRef(ev, _fTestDragSU->GetID(ev), aSURef);
  777.         StorageUnitSetValue(partSU, ev, sizeof(ODStorageUnitRef), aSURef);
  778. #endif
  779.  
  780.         if (partSU->Exists(ev, kODPropContents, kODAppleFileTEXT, 0) )
  781.         {
  782.             SURemoveValue(ev, partSU, kODPropContents, kODAppleFileTEXT);    // $$$$$ Right thing to do?
  783.             SURemoveValue(ev, partSU, kODPropContents, kODApplehfs);
  784.         }
  785.         
  786.         _fDirty = kODFalse;
  787.     
  788.     }
  789.     SOM_CATCH_ALL
  790.     SOM_ENDTRY
  791. }
  792.  
  793. //-------------------------------------------------------------------------
  794. // DragText: Release (Override)
  795. //-------------------------------------------------------------------------
  796.  
  797. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRelease(AppleTestDrag_DragText *somSelf, Environment *ev)
  798. {
  799.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  800.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","Release");
  801.  
  802.     parent_Release(somSelf, ev);
  803.     
  804.     if ( somSelf->GetRefCount(ev) == 0 )
  805.         somSelf->GetStorageUnit(ev)->GetDraft(ev)->ReleasePart(ev, _fPartWrapper);
  806.  
  807. }
  808.  
  809. //-------------------------------------------------------------------------
  810. // DragText: Release All (Override)
  811. //-------------------------------------------------------------------------
  812.  
  813. SOM_Scope void  SOMLINK AppleTestDrag_DragTextReleaseAll(AppleTestDrag_DragText *somSelf, Environment *ev)
  814. {
  815.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  816.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ReleaseAll");
  817.  
  818.     SOM_TRY
  819.  
  820.     // PRINT("DragText::ReleaseAll: draft refcount is %d\n", somSelf->GetStorageUnit(ev)->GetDraft(ev)->GetRefCount(ev));
  821.  
  822.     ODReleaseObject(ev,_fSrcLink.linkSource);
  823.     ODReleaseObject(ev,_fDstLink.link);
  824.  
  825.     ODReleaseObject(ev,_fTestDragSU);
  826.  
  827.     AppleTestDrag_DragText_parent_ODPart_ReleaseAll(somSelf,ev);
  828.  
  829.     SOM_CATCH_ALL
  830.     SOM_ENDTRY
  831. }
  832.  
  833. //-------------------------------------------------------------------------
  834. // From DragAndDrop protocol
  835. //-------------------------------------------------------------------------
  836.  
  837. SOM_Scope void  SOMLINK AppleTestDrag_DragTextFulfillPromise(AppleTestDrag_DragText *somSelf, Environment *ev,
  838.         ODStorageUnitView* promiseSUView)
  839. {
  840.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  841.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","FulfillPromise");
  842.  
  843. #ifdef TRYOUT_PROMISE
  844.     ODStorageUnit* su = promiseSUView->GetStorageUnit(ev)->GetDraft(ev)->CreateStorageUnit(ev);
  845.     su->Release(ev);
  846.     promiseSUView->Remove(ev);
  847. #endif
  848.  
  849. #ifdef TRYOUT_PROMISE_FILE
  850.     ODValueType valueType = promiseSUView->GetType(ev);
  851.     if (ODISOStrEqual(valueType, kODHFSPromise))
  852.     {
  853.         ODByteArray    ba;
  854.         short fileRefNum = 0;
  855.         
  856.         ODVolatile(ba);
  857.         ODVolatile(fileRefNum);
  858.         
  859.         SOM_TRY
  860.             const Str63 fileName = "\pDragText (TEXT)";
  861.             FSSpec dropFSSpec;
  862.             StorageUnitViewGetValue(promiseSUView, ev, sizeof(FSSpec), &dropFSSpec);
  863.             ODBlockMove(fileName, &(dropFSSpec.name), fileName[0]+ 1);
  864.             promiseSUView->SetOffset(ev, 0);
  865.             
  866.             ba._buffer = kODNULL;
  867.             ODStorageUnit* su = promiseSUView->GetStorageUnit(ev);
  868.             su->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  869.             long size = su->GetSize(ev);
  870.             size = su->GetValue(ev, size, &ba);
  871.             
  872.             OSErr err;
  873.             if (((err = FSpCreate(&dropFSSpec, 'ttxt','TEXT', smSystemScript)) == noErr) &&
  874.                 ((err = FSpOpenDF(&dropFSSpec, fsRdWrPerm, &fileRefNum)) == noErr) &&
  875.                 ((err = FSWrite(fileRefNum, &size, ba._buffer)) == noErr) &&
  876.                 ((err = FSClose(fileRefNum)) == noErr))
  877.             {
  878.                 StorageUnitViewSetValue(promiseSUView, ev, sizeof(FSSpec), &dropFSSpec);
  879.             }
  880.             else
  881.             {
  882.                 ODULong size = promiseSUView->GetSize(ev);
  883.                 promiseSUView->DeleteValue(ev, size);
  884.             }
  885.             ODDisposePtr(ba._buffer);
  886.             
  887.  
  888.         SOM_CATCH_ALL
  889.         
  890.             if (fileRefNum)
  891.                 FSClose(fileRefNum);
  892.             
  893.             ODDisposePtr(ba._buffer);
  894.             
  895.             ODSetSOMException(ev, 0);
  896.             
  897.         SOM_ENDTRY
  898.     }
  899.     ODDisposePtr(valueType);
  900. #endif
  901.  
  902. /*
  903.     StScrpHandle theStyl;
  904.  
  905.     theStyl = TEGetStyleScrapHandle(_fDocument->theTE);
  906.     if (theStyl)
  907.     {
  908.         ODLockHandle((ODHandle) theStyl);
  909.         StorageUnitSetValue(promiseSUView, ev, ODGetHandleSize((ODHandle) theStyl), (ODValue)*theStyl);
  910.         ODUnlockHandle((ODHandle) theStyl);
  911.         ODDisposeHandle((ODHandle) theStyl);
  912.     }
  913. */
  914. }
  915.  
  916. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDropCompleted(AppleTestDrag_DragText *somSelf, Environment *ev,
  917.         ODPart* destPart,
  918.         ODDropResult dropResult)
  919. {
  920.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  921.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DropCompleted");
  922.  
  923. ODUnused(destPart);
  924. ODUnused(dropResult);
  925. }
  926.  
  927. ODStatic void PrintContentKinds(Environment *ev, ODStorageUnit *su)
  928. {
  929.     if ( su->Exists(ev, kODPropContents, kODNULL, 0) )
  930.     {
  931.         su->Focus(ev, kODPropContents, kODPosUndefined, kODTypeAll, 0, kODPosUndefined);
  932.         ODULong numValues = su->CountValues(ev);    
  933.         ODULong kindIndex;
  934.         
  935.         for (kindIndex = 0; kindIndex < numValues; kindIndex++)
  936.         {
  937.             su->Focus(ev, kODPropContents, kODPosUndefined, kODTypeAll, 0, kODPosNextSib);
  938.             ODValueType kind = su->GetType(ev);
  939.             somPrintf("Content type: %s\n", kind);
  940.             ODDisposePtr(kind);
  941.         }
  942.     }
  943. }
  944.  
  945. SOM_Scope ODDragResult  SOMLINK AppleTestDrag_DragTextDragEnter(AppleTestDrag_DragText *somSelf, Environment *ev,
  946.         ODDragItemIterator* dragInfo,
  947.         ODFacet* facet,
  948.         ODPoint* where)
  949. {
  950.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  951.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DragEnter");
  952.  
  953. ODUnused(where);
  954.  
  955.     SOM_TRY
  956.  
  957.     ODStorageUnit *dragSU;
  958.  
  959.     if ( facet->GetFrame(ev)->GetLinkStatus(ev) == kODInLinkDestination )
  960.     {
  961.         _fDocument->canAccept = kODFalse;
  962.     }
  963.     else
  964.     {
  965.         _fDocument->canAccept = true;
  966.         for (dragSU = dragInfo->First(ev); dragSU; dragSU = dragInfo->Next(ev))
  967.         {
  968. #ifdef ODDebug
  969.             PRINT("DragText: DragEnter looking for content values\n");
  970.             PrintContentKinds(ev, dragSU);
  971. #endif
  972.             
  973.             if ( !dragSU->Exists(ev, kODPropContents, kODAppleTEXT, 0) )
  974.             {
  975.                 if( !dragSU->Exists(ev, kODPropContents, kODKindTestDrag, 0) )
  976.                 {
  977.                     if ( !dragSU->Exists(ev, kODPropContents, kODAppleFileTEXT, 0)
  978.                         || !dragSU->Exists(ev, kODPropContents, kODApplehfs, 0) )
  979.                     {
  980.                         _fDocument->canAccept = kODFalse;
  981.                         break;
  982.                     }
  983.                 }
  984.             }
  985.         }
  986.         _fDocument->caretTime = TickCount();
  987.         _fDocument->caretOffset = _fDocument->lastOffset = _fDocument->insertPosition = -1;
  988.         _fDocument->caretShow = true;
  989.         _fDocument->cursorInContent = false;
  990.     }
  991.     
  992.     return _fDocument->canAccept;
  993.  
  994.     SOM_CATCH_ALL
  995.     SOM_ENDTRY
  996.     return kODFalse;
  997. }
  998.  
  999. SOM_Scope ODDragResult  SOMLINK AppleTestDrag_DragTextDragWithin(AppleTestDrag_DragText *somSelf, Environment *ev,
  1000.         ODDragItemIterator* dragInfo,
  1001.         ODFacet* facet,
  1002.         ODPoint* where)
  1003. {
  1004.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1005.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DragWithin");
  1006.  
  1007. ODUnused(dragInfo);
  1008.  
  1009.     SOM_TRY
  1010.  
  1011.     short offset;
  1012.     long theTime = TickCount();
  1013.     GrafPtr    savedPort;
  1014.     ODULong attributes;
  1015.     DragReference theDrag;
  1016.     RgnHandle dragHilite;
  1017.     
  1018.     ODPoint windowWhere = *where;
  1019.     { TempODTransform wfTform = facet->AcquireWindowFrameTransform(ev, kODNULL);
  1020.       wfTform->TransformPoint(ev, &windowWhere);
  1021.     }
  1022.     
  1023.     if (!_fDocument->canAccept)
  1024.         return kODFalse;
  1025.  
  1026.     GetPort(&savedPort);
  1027.     SetPort(_fDocument->theWindow);
  1028.     
  1029.     somSelf->ClipThis(ev, facet);
  1030.     
  1031.     attributes = _fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  1032.     theDrag = _fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  1033.  
  1034.     if (PtInRect(windowWhere.AsQDPoint(), &(**(_fDocument->theTE)).viewRect))
  1035.     {
  1036.         if (!_fDocument->cursorInContent) 
  1037.         {
  1038.             dragHilite = ODNewRgn();
  1039.             RectRgn(dragHilite, &(**(_fDocument->theTE)).viewRect);
  1040.             ShowDragHilite(theDrag, dragHilite, true);
  1041.         }
  1042.         _fDocument->cursorInContent = true;
  1043.         
  1044.         offset = TEGetOffset(windowWhere.AsQDPoint(), _fDocument->theTE);
  1045.  
  1046.         if ((TEIsFrontOfLine(offset, _fDocument->theTE)) && (offset) &&            
  1047.             ((*((char **)((**(_fDocument->theTE)).hText)))[offset - 1] != 0x0D) &&
  1048.             (ff(TEGetPoint(offset - 1, _fDocument->theTE).h) < windowWhere.x))
  1049.             offset--;
  1050.         
  1051.         // If this is the source frame, do not allow tracking through the selection
  1052.         if ((attributes & kODDragIsInSourceFrame) &&
  1053.             (offset >= (**(_fDocument->theTE)).selStart) &&
  1054.             (offset <= (**(_fDocument->theTE)).selEnd))
  1055.             offset = -1;
  1056.         
  1057.         // Do not set insertion point within a link destination
  1058.         if ( (offset > _fDstLink.start) && (offset < _fDstLink.end) )
  1059.             offset = _fDocument->insertPosition;
  1060.         
  1061.         _fDocument->insertPosition = offset;
  1062.  
  1063.         //
  1064.         //    Reset flashing counter if the offset has moved.
  1065.         //
  1066.         if (offset != _fDocument->lastOffset) 
  1067.         {
  1068.             _fDocument->caretTime = theTime;
  1069.             _fDocument->caretShow = true;
  1070.         }
  1071.         _fDocument->lastOffset = offset;
  1072.  
  1073.         //
  1074.         //    Flash caret.
  1075.         //
  1076.         if (theTime - _fDocument->caretTime > GetCaretTime())
  1077.         {
  1078.             _fDocument->caretShow = !_fDocument->caretShow;
  1079.             _fDocument->caretTime = theTime;
  1080.         }
  1081.         if (!_fDocument->caretShow)
  1082.             offset = -1;
  1083.  
  1084.         //
  1085.         //    If offset has changed, move caret on screen.
  1086.         //
  1087.         if (offset != _fDocument->caretOffset) 
  1088.         {
  1089.             if (offset != -1)
  1090.                 DrawCaret(offset, _fDocument->theTE, 0L);
  1091.                 
  1092.             if (_fDocument->caretOffset != -1)
  1093.                 DrawCaret(_fDocument->caretOffset, _fDocument->theTE, 0L);
  1094.         }
  1095.         
  1096.         _fDocument->caretOffset = offset;
  1097.     }
  1098.     else
  1099.     {
  1100.         if (_fDocument->cursorInContent) 
  1101.         {
  1102.             HideDragHilite(theDrag);
  1103.         }
  1104.         _fDocument->cursorInContent = false;
  1105.         _fDocument->lastOffset = -1;
  1106.         _fDocument->insertPosition = -1;
  1107.     }
  1108.     
  1109.     SetPort(savedPort);
  1110.     
  1111.     return kODTrue;
  1112.  
  1113.     SOM_CATCH_ALL
  1114.     SOM_ENDTRY
  1115.     return kODFalse;
  1116. }
  1117.  
  1118. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDragLeave(AppleTestDrag_DragText *somSelf, Environment *ev,
  1119.         ODFacet* facet,
  1120.         ODPoint* where)
  1121. {
  1122.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1123.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DragLeave");
  1124.  
  1125. ODUnused(facet);
  1126. ODUnused(where);
  1127.  
  1128.     SOM_TRY
  1129.  
  1130.     GrafPtr    savedPort;
  1131.     ODULong attributes;
  1132.     DragReference theDrag;
  1133.     
  1134.     if (!_fDocument->canAccept)
  1135.         return;
  1136.  
  1137.     GetPort(&savedPort);
  1138.     SetPort(_fDocument->theWindow);
  1139.     
  1140.     theDrag = _fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  1141.     attributes = _fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  1142.  
  1143.     //
  1144.     //    If the caret is on the screen, remove it.
  1145.     //
  1146.     if (_fDocument->caretOffset != -1) 
  1147.     {
  1148.         DrawCaret(_fDocument->caretOffset, _fDocument->theTE, 0L);
  1149.         _fDocument->caretOffset = -1;
  1150.     }
  1151.  
  1152.     //
  1153.     //    Remove frame highlighting, if showing.
  1154.     //
  1155.     if (_fDocument->cursorInContent)
  1156.     {
  1157.         HideDragHilite(theDrag);
  1158.     }
  1159.     
  1160.     SetPort(savedPort);
  1161.  
  1162.     SOM_CATCH_ALL
  1163.     SOM_ENDTRY
  1164. }
  1165.  
  1166. SOM_Scope ODDropResult  SOMLINK AppleTestDrag_DragTextDrop(AppleTestDrag_DragText *somSelf, Environment *ev,
  1167.         ODDragItemIterator* dropInfo,
  1168.         ODFacet* facet,
  1169.         ODPoint* where)
  1170. {
  1171.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1172.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","Drop");
  1173.  
  1174.     SOM_TRY
  1175.  
  1176.     GrafPtr                savedPort;
  1177.     Size                textSize, stylSize;
  1178.     short                selStart, selEnd, moveText;
  1179.     //WindowOffscreen        *theOffscreen;
  1180.     Rect                theRect, srcRect;
  1181.     Point                thePoint;
  1182.     ODULong            attributes;
  1183.     DragReference       theDrag;
  1184.     ODStorageUnit     *dropSU;
  1185.     OSErr                error = noErr;
  1186.     short                fileRefNum = 0;
  1187.     ODPtr                fileData = kODNULL;
  1188.     ODPtr                textData = kODNULL;
  1189.     TEHandle            tempTE = kODNULL;
  1190.     StScrpHandle        stylHandle = kODNULL;
  1191.     ODDropResult        dropResult = kODDropCopy;
  1192.     ODBoolean            done = kODFalse;
  1193.     
  1194. // $$$$$ OBSOLETE: Volatile(fileData);
  1195. // $$$$$ OBSOLETE: Volatile(textData);
  1196. // $$$$$ OBSOLETE: Volatile(stylHandle);
  1197. // $$$$$ OBSOLETE: Volatile(tempTE);
  1198. // $$$$$ OBSOLETE: Volatile(fileRefNum);
  1199. // $$$$$ OBSOLETE: Volatile(exit);
  1200.  
  1201.     if ((!_fDocument->canAccept) || (_fDocument->insertPosition == -1))
  1202.         return(kODDropFail);
  1203.  
  1204.     if ( OneDragItem(ev, dropInfo) )
  1205.     {
  1206.         // Call DropOneItem unless the storage unit contains an Applehfs value,
  1207.         // in which case just let the "old" code handle it.
  1208.         if ( !(dropInfo->First(ev)->Exists(ev, kODPropContents, kODApplehfs, 0)) )
  1209.             return somSelf->DropOneItem(ev, dropInfo->First(ev), facet, where);
  1210.     }
  1211.  
  1212.     GetPort(&savedPort);
  1213.     SetPort(_fDocument->theWindow);
  1214.     
  1215.     attributes = _fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  1216.     moveText = (attributes & kODDragIsInSourceFrame) && (attributes & kODDropIsMove);
  1217.     if (attributes & kODDropIsMove)
  1218.         dropResult = kODDropMove;
  1219.     theDrag = _fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  1220.  
  1221.     //
  1222.     //    Loop through all of the drag items contained in this drag and collect the text
  1223.     //    into the tempTE record.
  1224.     //
  1225.  
  1226.     SetRect(&theRect, 0, 0, 0, 0);
  1227.     tempTE = TEStyleNew(&theRect, &theRect);
  1228.  
  1229.     TRY
  1230.         for (dropSU = dropInfo->First(ev); dropSU && !done ; dropSU = dropInfo->Next(ev))
  1231.     {
  1232.         //
  1233.         //  If canAccept is true then 'TEXT' or 'hfs' must be present in all items
  1234.         //
  1235.         if (dropSU->Exists(ev, kODPropContents, kODAppleTEXT, 0))
  1236.         {
  1237.             dropSU->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  1238.             textSize = dropSU->GetSize(ev);
  1239.             textData = ODNewPtr(textSize, kDefaultHeapID);
  1240.             StorageUnitGetValue(dropSU, ev, textSize, (ODValue)textData);
  1241.     
  1242.             //
  1243.             //    Check for optional styl data for the TEXT.
  1244.             //
  1245.             if (dropSU->Exists(ev, kODPropContents, kODApplestyl, 0))
  1246.             {
  1247.                 dropSU->Focus(ev, kODPropContents, kODPosUndefined, kODApplestyl, 0, kODPosUndefined);
  1248.                 stylSize = dropSU->GetSize(ev);
  1249.                 stylHandle = (StScrpHandle) ODNewHandle(stylSize);
  1250.                 StorageUnitGetValue(dropSU, ev, stylSize, (ODValue) ODLockHandle((ODHandle) stylHandle));
  1251.                 ODUnlockHandle((ODHandle) stylHandle);
  1252.             }
  1253.             done = kODTrue;
  1254.         }
  1255.         else if (dropSU->Exists(ev, kODPropContents, kODApplehfs, 0))
  1256.         {
  1257.             FSSpec fileSpec;
  1258.             if ( GetFSSpecFromHFSProperty(ev, dropSU, &fileSpec) )
  1259.             {
  1260.                 // $$$$$ Should modify to use InsertFromTextFile.
  1261.                 THROW_IF_ERROR(FSpOpenDF(&fileSpec, fsRdPerm, &fileRefNum));
  1262.                 THROW_IF_ERROR(GetEOF(fileRefNum,&textSize));
  1263.  
  1264.                 if (textSize == 0)
  1265.                     THROW(kODErrUndefined);
  1266.                 else if ( textSize < 0 )
  1267.                     THROW(kDragTextBogusTextSize);
  1268.                 else if ( textSize + (**tempTE).teLength + (**(_fDocument->theTE)).teLength > kMaxTextSize )
  1269.                     THROW(kDragTextErrTooMuchText);
  1270.                     
  1271.                 textData = ODNewPtr(textSize, kDefaultHeapID);
  1272.                 
  1273.                 THROW_IF_ERROR(SetFPos(fileRefNum,fsFromStart,0));
  1274.                 THROW_IF_ERROR(FSRead(fileRefNum,&textSize,textData));
  1275.                 THROW_IF_ERROR(FSClose(fileRefNum));
  1276.                 done = kODTrue;
  1277.             }
  1278.             else
  1279.                 THROW(kDragTextErrCantReadTEXTFile);
  1280.         }
  1281.         //
  1282.         //    Insert this drag item's text into the tempTE.
  1283.         //
  1284. //        TESetSelect(32767, 32767, tempTE);
  1285.         TEStyleInsert(textData, textSize, stylHandle, tempTE);
  1286.  
  1287.             if (textData)
  1288.                 ODDisposePtr(textData);
  1289.             if (stylHandle)
  1290.                 ODDisposeHandle((ODHandle) stylHandle);
  1291.         }
  1292.         
  1293.     CATCH_ALL
  1294.         if ( ErrorCode() == kDragTextErrTooMuchText )
  1295.             ShowInfoAlert(ev, "\pToo much text to drop here.", facet->GetFrame(ev));
  1296.         else
  1297.             ShowErrorAlert(ev, "\pError during drop.", ErrorCode(), facet->GetFrame(ev));
  1298.  
  1299.         if (tempTE != kODNULL)
  1300.             TEDispose(tempTE);
  1301.         if (fileData != kODNULL)
  1302.             ODDisposePtr(fileData);
  1303.         if (stylHandle != kODNULL)
  1304.             ODDisposeHandle((ODHandle) stylHandle);
  1305.         if (fileRefNum != 0)
  1306.             FSClose(fileRefNum);
  1307.         dropResult = kODDropFail;
  1308.     ENDTRY
  1309.     
  1310.     if ( (dropResult == kODDropCopy) || (dropResult == kODDropMove) ) {
  1311.  
  1312.     //
  1313.     //    Pull the TEXT and styl data out of the tempTE handle.
  1314.     //
  1315.     textSize = (**tempTE).teLength;
  1316.     textData = ODNewPtr(textSize, kDefaultHeapID);
  1317.     if (!textData) 
  1318.     {
  1319.         WARN("NewPtr error");
  1320.         TEDispose(tempTE);
  1321.         return(kODDropFail);
  1322.     }
  1323.     ODBlockMove(*(**tempTE).hText, textData, textSize);
  1324.     TESetSelect(0, 32767, tempTE);
  1325.     stylHandle = TEGetStyleScrapHandle(tempTE);
  1326.     TEDispose(tempTE);
  1327.  
  1328.     if (textSize)
  1329.     {
  1330.         //
  1331.         //    If the caret or highlighting is on the screen, remove it/them.
  1332.         //
  1333.         if (_fDocument->caretOffset != -1) 
  1334.         {
  1335.             DrawCaret(_fDocument->caretOffset, _fDocument->theTE, 0L);
  1336.             _fDocument->caretOffset = -1;
  1337.         }
  1338.         if (_fDocument->cursorInContent)
  1339.         {
  1340.             HideDragHilite(theDrag);
  1341.         }
  1342.  
  1343.         //
  1344.         //    If the frame is not active, must activate TE before inserting
  1345.         //    text or the background hilite will not update correctly.
  1346.         //
  1347.         if (!_fDocument->frameIsActive)
  1348.             TEActivate(_fDocument->theTE);
  1349.  
  1350.         //
  1351.         //    If this window is also the sender, delete source selection if no option key.
  1352.         //    
  1353.         if (moveText)
  1354.         {
  1355.             selStart = (**(_fDocument->theTE)).selStart;
  1356.             selEnd   = (**(_fDocument->theTE)).selEnd;
  1357.             if ( WhiteSpaceAtOffset(selStart - 1, _fDocument->theTE) &&
  1358.                 !WhiteSpaceAtOffset(selStart, _fDocument->theTE) &&
  1359.                 !WhiteSpaceAtOffset(selEnd - 1, _fDocument->theTE) &&
  1360.                  WhiteSpaceAtOffset(selEnd, _fDocument->theTE)) {
  1361.                  
  1362.                  if (GetCharAtOffset(selEnd, _fDocument->theTE) == ' ')
  1363.                     (**(_fDocument->theTE)).selEnd++;
  1364.             }
  1365.             if (_fDocument->insertPosition > selStart) {
  1366.                 _fDocument->insertPosition -= (ev, (**(_fDocument->theTE)).selEnd -
  1367.                                               (**(_fDocument->theTE)).selStart);
  1368.             }
  1369.             srcRect = (**_fDocument->hiliteRgn).rgnBBox;
  1370.             TEDelete(_fDocument->theTE);
  1371.         }
  1372.         
  1373.         InsertTextAtOffset(_fDocument->insertPosition, (char*) textData, textSize, stylHandle, _fDocument->theTE);
  1374.         
  1375.         TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  1376.         
  1377.         if (moveText)
  1378.         {
  1379.             theRect = (**_fDocument->hiliteRgn).rgnBBox;
  1380.             SetPt(&thePoint,0,0);
  1381.             LocalToGlobal(&thePoint);
  1382.             OffsetRect(&srcRect, thePoint.h, thePoint.v);
  1383.             OffsetRect(&theRect, thePoint.h, thePoint.v);
  1384.             ZoomRects(&srcRect, &theRect, 12, zoomDecelerate);
  1385.         }        
  1386.  
  1387.         _fDirty = kODTrue;
  1388.         _fDraft->SetChangedFromPrev(ev);
  1389.     }
  1390.  
  1391.         if (textData)
  1392.             ODDisposePtr(textData);
  1393.         if (fileData)
  1394.             ODDisposePtr(fileData);
  1395.     
  1396.         if (stylHandle)
  1397.             ODDisposeHandle((ODHandle) stylHandle);
  1398.     
  1399.     //
  1400.     //    Undo the TEActivate, if needed.
  1401.     //
  1402.     if (!_fDocument->frameIsActive)
  1403.         TEDeactivate(_fDocument->theTE);
  1404.  
  1405.     //
  1406.     //  Signal the draw method to update the scroll bars
  1407.     //
  1408.     _fFrameShapeChanged = true;
  1409.  
  1410.     facet->Invalidate(ev, kODNULL, kODNULL);
  1411.     }
  1412.     else
  1413.     {
  1414.         // Remove the drop hilite
  1415.         if (_fDocument->cursorInContent)
  1416.             HideDragHilite(theDrag);
  1417.     }
  1418.  
  1419.     SetPort(savedPort);
  1420.     return dropResult;
  1421.  
  1422.     SOM_CATCH_ALL
  1423.     SOM_ENDTRY
  1424.     return kODDropFail;
  1425. }
  1426.  
  1427. SOM_Scope ODDropResult  SOMLINK AppleTestDrag_DragTextDropOneItem(AppleTestDrag_DragText *somSelf, Environment *ev,
  1428.         ODStorageUnit* dropSU,
  1429.         ODFacet* facet,
  1430.         ODPoint* where)
  1431. {
  1432.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1433.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DropOneItem");
  1434.  
  1435.     DragReference       theDrag;
  1436.     ODDropResult        dropResult;
  1437.     ODBoolean            isMove;
  1438.     ODBoolean            pasteAs = kODFalse;
  1439.     ODCloneKind            cloneKind;
  1440.     
  1441.     theDrag = _fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  1442.  
  1443.     ODULong    attributes = _fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  1444.     isMove = attributes & kODDropIsMove;
  1445.     pasteAs = attributes & kODDropIsPasteAs;
  1446.     
  1447.     cloneKind = ( isMove ? kODCloneDropMove : kODCloneDropCopy );
  1448.  
  1449.     dropResult = ( isMove ? kODDropMove : kODDropCopy );
  1450.     
  1451.     TRY
  1452. #ifdef SHOW_DROP_PASTE_AS_DIALOG
  1453.         if ( pasteAs )
  1454.         {
  1455.             ODPasteAsResult pasteAsRslt;
  1456.                 pasteAsRslt.selectedKind = kODNULL;
  1457.                 pasteAsRslt.translateKind = kODNULL;
  1458.                 pasteAsRslt.editor = kODNULL;
  1459.             
  1460.             TempODPasteAsResult tempResult = &pasteAsRslt; 
  1461.             // DMc: ensure deletion of selectedKind, translateKind, editor
  1462.                 
  1463.             // $$$$$ This test that the drop originated with this part
  1464.             // fails if there are more than one display frame.
  1465.             ODULong attributes = _fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  1466.             ODBoolean isMyContent = (attributes & kODDragIsInSourceFrame);
  1467.             ODBoolean enablePasteLink = /* (!isMove) && */
  1468.                 (somSelf->CanPasteLink(ev,
  1469.                                         dropSU,
  1470.                                         isMyContent,
  1471.                                         _fDocument->insertPosition, 
  1472.                                         _fDocument->insertPosition));
  1473.  
  1474.             GrafPtr savePort;
  1475.             GetPort(&savePort);    // $$$$$ ShowPasteAsDialog changes the current port
  1476.             if ( _fSession->GetDragAndDrop(ev)->ShowPasteAsDialog(ev,
  1477.                         enablePasteLink,
  1478.                         kODPasteAsMergeOnly,
  1479.                         facet,
  1480.                         _fSession->Tokenize(ev, kODViewAsFrame),
  1481.                         dropSU,
  1482.                         &pasteAsRslt) )
  1483.             {
  1484.                 somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  1485.                 if ( pasteAsRslt.pasteLinkSetting != kODFalse )
  1486.                 {
  1487.                     somSelf->DoDropPasteLink(ev, dropSU, pasteAsRslt.selectedKind, facet, where);
  1488.                     dropResult = kODDropCopy;
  1489.                 }
  1490.                 else
  1491.                     somSelf->DoDropPaste(ev, dropSU, cloneKind, pasteAsRslt.selectedKind, facet, where);
  1492.             }
  1493.             else
  1494.             {
  1495.                 // User cancelled the drop
  1496.                 dropResult = kODDropFail;
  1497.                 
  1498.                 // Remove the drop hilight
  1499.                 SetPort(savePort);    // $$$$$ ShowPasteAsDialog changes the current port
  1500.                 HideDragHilite(theDrag);
  1501.             }
  1502.         }
  1503.         else
  1504. #endif
  1505.         {
  1506.             somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  1507.             somSelf->DoDropPaste(ev, dropSU, cloneKind, (ODType) kODNULL, facet, where);
  1508.         }
  1509.     CATCH_ALL
  1510.         WARN("DropOneItem: Drop failed.");
  1511.         dropResult = kODDropFail;
  1512.     ENDTRY
  1513.  
  1514.     return dropResult;
  1515. }
  1516.  
  1517. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCommonDropProlog(AppleTestDrag_DragText *somSelf, Environment *ev,
  1518.         ODCloneKind cloneKind)
  1519. {
  1520.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1521.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CommonDropProlog");
  1522.  
  1523.     SOM_TRY
  1524.  
  1525.     ODULong attributes = _fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  1526.     DragReference theDrag = _fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  1527.  
  1528.     //    If the caret or highlighting is on the screen, remove it/them.
  1529.     if (_fDocument->caretOffset != -1) 
  1530.     {
  1531.         DrawCaret(_fDocument->caretOffset, _fDocument->theTE, 0L);
  1532.         _fDocument->caretOffset = -1;
  1533.     }
  1534.     if (_fDocument->cursorInContent)
  1535.     {
  1536.         HideDragHilite(theDrag);
  1537.     }
  1538.  
  1539.     //    If the frame is not active, must activate TE before inserting
  1540.     //    text or the background hilite will not update correctly.
  1541.     if (!_fDocument->frameIsActive)
  1542.         TEActivate(_fDocument->theTE);
  1543.  
  1544.     // If this is a move within the same part, abandon a moved link source or
  1545.     // destination now.  This has to be done only because this part supports only 
  1546.     // one link source and one link destination.
  1547.     if ( (cloneKind == kODCloneDropMove) && (attributes & kODDragIsInSourceFrame) )
  1548.     {
  1549.         // If the selection includes a link, assume its being moved.
  1550.         ODSShort selStart = (**(_fDocument->theTE)).selStart;
  1551.         ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  1552.  
  1553.         if ( _fSrcLink.linkSource != (ODLinkSource*) kODNULL )
  1554.         {
  1555.             if ( IncludedInRange(_fSrcLink.start, _fSrcLink.end, selStart, selEnd) )
  1556.             {
  1557.                 // Since we're just moving the source of the link, just allow
  1558.                 // us to make a new one.
  1559.                 _fSrcLink.linkSource->Release(ev);
  1560.                 _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  1561.                 _fSrcLink.start = -1;
  1562.                 _fSrcLink.end = -1;
  1563.                 _fSrcLink.change = kODUnknownUpdate;
  1564.             }
  1565.         }
  1566.  
  1567.         if ( _fDstLink.link != (ODLink*) kODNULL )
  1568.         {
  1569.             if ( IncludedInRange(_fDstLink.start, _fDstLink.end, selStart, selEnd) )
  1570.             {
  1571.                 if ( _fDstLink.info.autoUpdate )
  1572.                     _fDstLink.link->UnregisterDependent(ev, _fPartWrapper);
  1573.                 _fDstLink.link->Release(ev);
  1574.                 _fDstLink.link = (ODLink*) kODNULL;
  1575.                 _fDstLink.start = -1;
  1576.                 _fDstLink.end = -1;
  1577.             }
  1578.         }
  1579.     }
  1580.  
  1581.     // Change the selection to the insertion position.
  1582.     TESetSelect(_fDocument->insertPosition, _fDocument->insertPosition, _fDocument->theTE);
  1583.  
  1584.     SOM_CATCH_ALL
  1585.     SOM_ENDTRY
  1586. }
  1587.  
  1588. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDoDropPaste(AppleTestDrag_DragText *somSelf, Environment *ev,
  1589.                     ODStorageUnit*    dropSU,
  1590.                     ODCloneKind        cloneKind,
  1591.                     ODType            desiredKind,
  1592.                     ODFacet*        facet, 
  1593.                     ODPoint*        where)
  1594. {
  1595.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1596.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoDropPaste");
  1597.  
  1598. ODUnused(where);
  1599.  
  1600.     SOM_TRY
  1601.  
  1602.     GrafPtr savedPort;
  1603.  
  1604.     GetPort(&savedPort);
  1605.     SetPort(_fDocument->theWindow);
  1606.  
  1607.     somSelf->CommonDropProlog(ev, cloneKind);
  1608.  
  1609.     ODSShort dropStart, dropEnd;
  1610.  
  1611.     TRY
  1612.  
  1613.         dropStart = (**(_fDocument->theTE)).selStart;
  1614.  
  1615. #ifdef SUPPORT_TESTDRAG_KIND
  1616.         if ( PasteThisKind (ev, dropSU, kODKindTestDrag, desiredKind) )
  1617.         {
  1618.             somSelf->OpenWhitespace(ev, dropSU, kODKindTestDrag);
  1619.             somSelf->IncorporateTestDragContent(ev, dropSU, _fSession->UniqueUpdateID(ev), cloneKind);
  1620.         }
  1621.         else 
  1622. #endif
  1623.         if ( PasteThisKind (ev, dropSU, kODApplestxt, desiredKind)
  1624.             ||
  1625.              PasteThisKind (ev, dropSU, kODApplestyl, desiredKind) )
  1626.         {
  1627.             somSelf->OpenWhitespace(ev, dropSU, kODAppleTEXT);
  1628.             somSelf->IncorporateStyledTextContent(ev, dropSU, _fSession->UniqueUpdateID(ev));
  1629.         }
  1630.         else if ( PasteThisKind (ev, dropSU, kODAppleTEXT, desiredKind) )
  1631.         {
  1632.             somSelf->OpenWhitespace(ev, dropSU, kODAppleTEXT);
  1633.             somSelf->IncorporateTextContent(ev, dropSU, _fSession->UniqueUpdateID(ev));
  1634.         }
  1635.  
  1636.         dropEnd = (**(_fDocument->theTE)).selEnd;
  1637.         TESetSelect(dropStart, dropEnd, _fDocument->theTE);
  1638.         TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  1639.  
  1640.     CATCH_ALL
  1641.         //    Undo the TEActivate, if needed.
  1642.         if (!_fDocument->frameIsActive)
  1643.             TEDeactivate(_fDocument->theTE);
  1644.         
  1645.         RERAISE;
  1646.     ENDTRY
  1647.  
  1648.     //    Undo the TEActivate, if needed.
  1649.     if (!_fDocument->frameIsActive)
  1650.         TEDeactivate(_fDocument->theTE);
  1651.  
  1652.     //  Signal the draw method to update the scroll bars
  1653.     _fFrameShapeChanged = true;
  1654.  
  1655.     facet->Invalidate(ev, kODNULL, kODNULL);
  1656.  
  1657.     SOM_CATCH_ALL
  1658.     SOM_ENDTRY
  1659. }
  1660.  
  1661. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDoDropPasteLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  1662.         ODStorageUnit* dropSU,
  1663.         ODType desiredKind,
  1664.         ODFacet* facet,
  1665.         ODPoint* where)
  1666. {
  1667.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1668.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoDropPasteLink");
  1669.  
  1670. ODUnused(facet);
  1671. ODUnused(where);
  1672.  
  1673.     SOM_TRY
  1674.  
  1675.     GrafPtr savedPort;
  1676.  
  1677.     GetPort(&savedPort);
  1678.     SetPort(_fDocument->theWindow);
  1679.  
  1680.     somSelf->CommonDropProlog(ev, kODCloneDropCopy);
  1681.  
  1682.     ODSShort dropStart = (**(_fDocument->theTE)).selStart;
  1683.  
  1684.     somSelf->PasteLinkFromSU(ev, facet->GetFrame(ev), desiredKind, dropSU);
  1685.  
  1686.     // If the source of the link was from this part, remove any link spec from the clipboard
  1687.     // $$$$$ If the link spec on the clipboard happens to refer to the same content as the
  1688.     // link just created, there is no need to remove it.
  1689.     somSelf->RemoveLinkSpec(ev);
  1690.  
  1691.     ODSShort dropEnd = (**(_fDocument->theTE)).selEnd;
  1692.     TESetSelect(dropStart, dropEnd, _fDocument->theTE);
  1693.  
  1694.     //    Undo the TEActivate, if needed.
  1695.     if (!_fDocument->frameIsActive)
  1696.         TEDeactivate(_fDocument->theTE);
  1697.  
  1698.     SOM_CATCH_ALL
  1699.     SOM_ENDTRY
  1700. }
  1701.  
  1702. SOM_Scope void  SOMLINK AppleTestDrag_DragTextOpenWhitespace(AppleTestDrag_DragText *somSelf, Environment *ev,
  1703.         ODStorageUnit* su, ODValueType kind)
  1704. {
  1705.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1706.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","OpenWhitespace");
  1707.  
  1708.     SOM_TRY
  1709.  
  1710.     char firstChar = PeekIntoValue(ev, su, kind, 0);
  1711.     char lastChar = PeekIntoValue(ev, su, kind, -1);
  1712.     somSelf->InsertWhitespace(ev, firstChar, lastChar);
  1713.  
  1714.     SOM_CATCH_ALL
  1715.     SOM_ENDTRY
  1716. }
  1717.  
  1718. SOM_Scope void  SOMLINK AppleTestDrag_DragTextInsertWhitespace(AppleTestDrag_DragText *somSelf, Environment *ev,
  1719.         char firstChar, char lastChar)
  1720. {
  1721.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1722.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InsertWhitespace");
  1723.  
  1724.     SOM_TRY
  1725.  
  1726.     TEHandle theTE = _fDocument->theTE;
  1727.  
  1728.     ODSShort selStart = (**theTE).selStart;
  1729.     ODSShort selEnd = (**theTE).selEnd;
  1730.  
  1731.     //
  1732.     //    If inserting at the end of a word and the selection does not begin with
  1733.     //    a space, insert a space before the insertion.
  1734.     //
  1735.  
  1736.     if (!WhiteSpaceAtOffset(selStart - 1, theTE) &&
  1737.          WhiteSpaceAtOffset(selStart, theTE) &&
  1738.         !WhiteSpace(firstChar)) {
  1739.  
  1740.         TESetSelect(selStart, selStart, theTE);
  1741.         TEKey(' ', theTE);
  1742.         somSelf->AdjustLinks(ev, selStart, selStart, selStart+1, _fSession->UniqueUpdateID(ev));
  1743.         selStart++;
  1744.         selEnd++;
  1745.     }
  1746.  
  1747.     //
  1748.     //    If inserting at the beginning of a word and the selection does not end
  1749.     //    with a space, insert a space after the insertion.
  1750.     //
  1751.  
  1752.     if ( WhiteSpaceAtOffset(selEnd - 1, theTE) &&
  1753.         !WhiteSpaceAtOffset(selEnd, theTE) &&
  1754.         !WhiteSpace(lastChar)) {
  1755.  
  1756.         TESetSelect(selEnd, selEnd, theTE);
  1757.         TEKey(' ', theTE);
  1758.         somSelf->AdjustLinks(ev, selEnd, selEnd, selEnd+1, _fSession->UniqueUpdateID(ev));
  1759.     }
  1760.  
  1761.     // Restore the selection
  1762.     TESetSelect(selStart, selEnd, theTE);
  1763.  
  1764.     SOM_CATCH_ALL
  1765.     SOM_ENDTRY
  1766. }
  1767.  
  1768. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDisplayFrameAdded(AppleTestDrag_DragText *somSelf, Environment *ev,
  1769.         ODFrame* frame)
  1770. {
  1771.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1772.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DisplayFrameAdded");
  1773.  
  1774.     SOM_TRY
  1775.  
  1776.     // If the first display frame is being added, register any link destination
  1777.     if (_fDisplayFrames->Count() == 0)
  1778.     {
  1779.         if ( _fDstLink.link != ((ODLink*) kODNULL) )
  1780.             if ( _fDstLink.info.autoUpdate )
  1781.                 _fDstLink.link->RegisterDependent(ev, _fPartWrapper, _fDstLink.info.change);
  1782.     }
  1783.  
  1784.     if (frame->GetViewType(ev) == kODNullTypeToken)                        // if frame view is set don't change it
  1785.         frame->SetViewType(ev, _fSession->Tokenize(ev, kODViewAsFrame));        // if not, make it viewasframe
  1786.  
  1787.     frame->SetDroppable(ev, kODTrue);
  1788.  
  1789.     _fDisplayFrames->AddLast(frame);
  1790.     frame->Acquire(ev);
  1791.  
  1792.     if ( frame->IsRoot(ev) )
  1793.         _fNeedsFoci = kODTrue;
  1794.  
  1795.     SOM_CATCH_ALL
  1796.     SOM_ENDTRY
  1797. }
  1798.  
  1799. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDisplayFrameRemoved(AppleTestDrag_DragText *somSelf, Environment *ev,
  1800.         ODFrame* frame)
  1801. {
  1802.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1803.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DisplayFrameRemoved");
  1804.  
  1805.     SOM_TRY
  1806.  
  1807.     if (_fDisplayFrames->Contains(frame))
  1808.     {
  1809.         somSelf->DeactivateFrame(ev, frame);
  1810.         _fDisplayFrames->Remove(frame);
  1811.         frame->Release(ev);
  1812.         
  1813.         // If the last display frame was removed, unregister any link destination
  1814.         if (_fDisplayFrames->Count() == 0)
  1815.         {
  1816.             if ( _fDstLink.link != ((ODLink*) kODNULL) )
  1817.                 if ( _fDstLink.info.autoUpdate )
  1818.                     _fDstLink.link->UnregisterDependent(ev, _fPartWrapper);
  1819.         }
  1820.     }
  1821.     else
  1822.     {
  1823.         THROW(kODErrInvalidFrame);
  1824.     }
  1825.  
  1826.     SOM_CATCH_ALL
  1827.     SOM_ENDTRY
  1828. }
  1829.  
  1830. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDisplayFrameConnected(AppleTestDrag_DragText *somSelf, Environment *ev,
  1831.         ODFrame* frame)
  1832. {
  1833.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1834.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DisplayFrameConnected");
  1835.  
  1836.     SOM_TRY
  1837.  
  1838.     frame->SetDroppable(ev, kODTrue);
  1839.  
  1840.     if ( !_fDisplayFrames->Contains(frame) )
  1841.     {
  1842.         frame->Acquire(ev);
  1843.         _fDisplayFrames->AddLast((ElementType) frame);
  1844.     }
  1845.  
  1846.     if ( frame->IsRoot(ev) )
  1847.         _fNeedsFoci = kODTrue;
  1848.  
  1849.     SOM_CATCH_ALL
  1850.     SOM_ENDTRY
  1851. }
  1852.  
  1853. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDisplayFrameClosed(AppleTestDrag_DragText *somSelf, Environment *ev,
  1854.         ODFrame* frame)
  1855. {
  1856.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1857.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DisplayFrameClosed");
  1858.  
  1859.     SOM_TRY
  1860.  
  1861.     if (_fDisplayFrames->Contains(frame))
  1862.     {
  1863.         somSelf->DeactivateFrame(ev, frame);
  1864.         _fDisplayFrames->Remove(frame);
  1865.         frame->Release(ev);
  1866.         
  1867.         // If the last display frame was closed, unregister any link destination
  1868.         if (_fDisplayFrames->Count() == 0)
  1869.         {
  1870.             if ( _fDstLink.link != ((ODLink*) kODNULL) )
  1871.                 if ( _fDstLink.info.autoUpdate )
  1872.                     _fDstLink.link->UnregisterDependent(ev, _fPartWrapper);
  1873.         }
  1874.     }
  1875.     else
  1876.     {
  1877.         THROW(kODErrInvalidFrame);
  1878.     }
  1879.  
  1880.     SOM_CATCH_ALL
  1881.     SOM_ENDTRY
  1882. }
  1883.  
  1884. SOM_Scope void  SOMLINK AppleTestDrag_DragTextGeometryChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  1885.         ODFacet* facet, ODBoolean clipShapeChanged, ODBoolean externalTransformChanged)
  1886. {
  1887.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1888.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","GeometryChanged");
  1889.  
  1890. ODUnused(facet);
  1891.     
  1892.     SOM_TRY
  1893.  
  1894.     somSelf->PositionDocumentParts(ev);
  1895.     somSelf->AdjustScrollBar(ev);
  1896.     TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  1897.     _fFrameShapeChanged = false;
  1898.  
  1899.     SOM_CATCH_ALL
  1900.     SOM_ENDTRY
  1901. }
  1902.  
  1903. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDraw(AppleTestDrag_DragText *somSelf, Environment *ev,
  1904.         ODFacet* facet,
  1905.         ODShape* invalidShape)
  1906. {
  1907.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1908.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","Draw");
  1909.  
  1910. ODUnused(invalidShape);
  1911.     
  1912.     SOM_TRY
  1913.  
  1914.     ODPoint xForm;
  1915.     ODShape* clipShape = kODNULL;
  1916.     ODShape* frameShape = kODNULL;
  1917.     RgnHandle clipRgn, saveClip, frameRgn;
  1918.     Rect bounds;
  1919.     GrafPtr    savedQDPort, oldTEPort;
  1920.     
  1921.     ODBoolean drawing = facet->GetCanvas(ev)->IsDynamic(ev);
  1922.     
  1923.     if (facet)
  1924.     {
  1925.         GetPort(&savedQDPort);
  1926.         if (drawing)
  1927.         {
  1928.             SetPort(facet->GetCanvas(ev)->GetQDPort(ev));
  1929.         }
  1930.         else
  1931.         {
  1932.             oldTEPort = (**(_fDocument->theTE)).inPort;
  1933.             (**(_fDocument->theTE)).inPort = facet->GetCanvas(ev)->GetQDPort(ev);
  1934.         }
  1935.         SetOrigin(0,0);
  1936.  
  1937.         xForm = GetAggregateTranslation(ev, facet);
  1938.  
  1939.         frameShape = facet->GetFrame(ev)->AcquireFrameShape(ev, kODNULL);
  1940.         frameRgn = frameShape->GetQDRegion(ev);
  1941.         clipShape = facet->AcquireClipShape(ev, kODNULL);
  1942.         clipRgn = clipShape->CopyQDRegion(ev);
  1943.         saveClip = ODNewRgn();
  1944.         
  1945.         GetClip(saveClip);
  1946.         bounds = (**frameRgn).rgnBBox;
  1947.         OffsetRect(&bounds, xForm.IntX(), xForm.IntY());
  1948.         OffsetRgn(clipRgn, xForm.IntX(), xForm.IntY());
  1949.         SetClip(clipRgn);
  1950.         EraseRect(&(**clipRgn).rgnBBox);
  1951.  
  1952.         // Draw the frame
  1953.         Rect theRect = (**(_fDocument->theTE)).viewRect;
  1954.         PenSize(1, 1);
  1955.         InsetRect(&theRect, -1, -1);
  1956.            PenPat(&ODQDGlobals.white);
  1957.         FrameRect(&theRect);
  1958.         InsetRect(&theRect, -1, -1);
  1959.            PenPat(&ODQDGlobals.black);
  1960.         FrameRect(&theRect);
  1961.         PenNormal();
  1962.  
  1963.         if ( drawing )
  1964.             DrawControls(_fDocument->theWindow);
  1965.         
  1966.         TEUpdate(&bounds, _fDocument->theTE);
  1967.  
  1968. #ifdef SHOW_LINK_BORDERS
  1969.         _fLinkBorderStyle = kLinkBorderOff;
  1970.         _fTimeBorderOff = TickCount();
  1971. #endif
  1972.  
  1973.         if (!drawing)                                    // restore the window port
  1974.             (**(_fDocument->theTE)).inPort = oldTEPort;
  1975.  
  1976.         SetPort(savedQDPort);    
  1977.         SetClip(saveClip);
  1978.         
  1979.         if (frameShape) frameShape->Release(ev);
  1980.         if (clipShape) clipShape->Release(ev);
  1981.         ODDisposeHandle((Handle)saveClip);
  1982.         ODDisposeHandle((Handle)clipRgn);
  1983.     }
  1984.  
  1985.     SOM_CATCH_ALL
  1986.     SOM_ENDTRY
  1987. }
  1988.  
  1989. SOM_Scope void  SOMLINK AppleTestDrag_DragTextShowLinkBorder(AppleTestDrag_DragText *somSelf, Environment *ev,
  1990.         ODFrame* frame,
  1991.         LinkBorderStyle borderStyle)
  1992. {
  1993.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  1994.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ShowLinkBorder");
  1995.  
  1996.     SOM_TRY
  1997.  
  1998.     if ( _fLinkBorderStyle != borderStyle )
  1999.     {
  2000.         ODFrameFacetIterator* fiter = frame->CreateFacetIterator(ev);
  2001.         if ( _fSrcLink.linkSource != (ODLinkSource*) kODNULL )
  2002.         {
  2003. #ifdef DEBUG_LINK_BORDERS
  2004.             PRINT("DragText %d: Show link source border\n", somSelf->GetID(ev));
  2005. #endif
  2006.             DrawLinkBorder(ev, fiter->First(ev), _fSrcLink.start, _fSrcLink.end, _fDocument->theTE, borderStyle);
  2007.         }
  2008.         if ( _fDstLink.link != ((ODLink*) kODNULL) )
  2009.         {
  2010. #ifdef DEBUG_LINK_BORDERS
  2011.             PRINT("DragText %d: Show link border\n", somSelf->GetID(ev));
  2012. #endif
  2013.             DrawLinkBorder(ev, fiter->First(ev), _fDstLink.start, _fDstLink.end, _fDocument->theTE, borderStyle);
  2014.         }
  2015.         ODDeleteObject(fiter);
  2016.         _fLinkBorderStyle = borderStyle;
  2017.     }
  2018.     if ( borderStyle == kLinkBorderOff )
  2019.         _fTimeBorderOff = TickCount();
  2020.  
  2021.     SOM_CATCH_ALL
  2022.     SOM_ENDTRY
  2023. }
  2024.  
  2025. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextHandleEvent(AppleTestDrag_DragText *somSelf, Environment *ev,
  2026.         ODEventData* event,
  2027.         ODFrame* frame,
  2028.         ODFacet* facet,
  2029.         ODEventInfo* eventInfo)
  2030. {
  2031.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2032.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleEvent");
  2033.  
  2034.     SOM_TRY
  2035.  
  2036.     ODBoolean handled = kODFalse;
  2037.     
  2038.     switch (event->what)
  2039.     {
  2040.         case nullEvent:
  2041.             somSelf->Idle(ev, frame);
  2042.             break;
  2043.         
  2044.         case mouseDown:
  2045.         case kODEvtBGMouseDown:
  2046.             {
  2047. //                PRINT("DragText %d: HandleEvent: mouseDown\n", somSelf->GetID(ev));
  2048.                 Point windowPoint = event->where;
  2049. //                GrafPtr curPort;
  2050. //                GetPort(&curPort);
  2051. //                SetPort(facet->AcquireWindow(ev)->GetPlatformWindow(ev));
  2052.                 SetOrigin(0,0);
  2053.                 GlobalToLocal(&windowPoint);
  2054.                 ODPoint windowODPoint(windowPoint);
  2055.                 handled = somSelf->HandleMouseDown(ev, facet, &windowODPoint, event);
  2056. //                SetPort(curPort);
  2057.             }
  2058.             break;
  2059.             
  2060.         case kODEvtMouseDownEmbedded:
  2061.             {
  2062.                 Point windowPoint = event->where;
  2063. //                GrafPtr curPort;
  2064. //                GetPort(&curPort);
  2065. //                SetPort(facet->AcquireWindow(ev)->GetPlatformWindow(ev));
  2066.                 SetOrigin(0,0);
  2067.                 GlobalToLocal(&windowPoint);
  2068.                 ODPoint windowODPoint(windowPoint);
  2069.                 handled = somSelf->HandleMouseDownInBorder(ev, facet, 
  2070.                     (ODFacet*) event->message, &windowODPoint, event);
  2071. //                SetPort(curPort);
  2072.             }
  2073.             break;
  2074.         
  2075.         case mouseUp:
  2076. //            PRINT("DragText %d: HandleEvent: mouseUp\n", somSelf->GetID(ev));
  2077.             if ( !_fHasFoci )
  2078.             {
  2079.                 somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  2080.                 GrafPtr curPort;
  2081.                 GetPort(&curPort);
  2082.                 SetPort(_fDocument->theWindow);
  2083.                 Point thePoint = event->where;
  2084.                 GlobalToLocal(&thePoint);
  2085.                 TEClick(thePoint, false, _fDocument->theTE);
  2086.                 somSelf->ExpandSelectionAroundLink(ev);
  2087.                 SetPort(curPort);
  2088.             }
  2089.             handled = kODTrue;
  2090.             break;
  2091.  
  2092.         case keyDown:
  2093.         case autoKey:
  2094.             handled = somSelf->HandleKeyDown(ev, frame, event);
  2095.             break;
  2096.     
  2097.         case activateEvt:
  2098.             handled = kODTrue;
  2099.             if ((event->modifiers & activeFlag) != 0)
  2100.             {
  2101. //                PRINT("DragText %d: HandleEvent: Activate\n", somSelf->GetID(ev));
  2102.                 somSelf->ActivatingWindow(ev, frame);
  2103.             }
  2104.             else
  2105.             {
  2106. //                PRINT("DragText %d: HandleEvent: Deactivate\n", somSelf->GetID(ev));
  2107.                 somSelf->DeactivatingWindow(ev, frame);
  2108.             }
  2109.             break;
  2110.  
  2111.         case keyUp:
  2112.             break;
  2113.  
  2114.         case osEvt:
  2115.             {
  2116.                 const short kSuspendResumeMessage = 0x01;    // Resume vs. suspend mask
  2117.                 unsigned char typeOSEvent = (unsigned char) (event->message >> 24) & 0x00FF;
  2118.                 if ( typeOSEvent == kSuspendResumeMessage ) 
  2119.                 {     
  2120.                     const short kResumeMask = 0x01;    // High byte suspend/resume event 
  2121.                     ODBoolean goingToBackground = (event->message & kResumeMask) == 0;
  2122.                     if ( goingToBackground )
  2123.                         somSelf->DeactivatingWindow(ev, frame);
  2124.                     else
  2125.                         somSelf->ActivatingWindow(ev, frame);
  2126.                 }
  2127.             }
  2128.             break;
  2129.     
  2130.         case kHighLevelEvent:
  2131.             break;
  2132.  
  2133.         case kODEvtMenu:
  2134.             handled = somSelf->HandleMenuEvent(ev, frame, event);
  2135.             break;
  2136.  
  2137.         case kODEvtMouseEnter:
  2138. #if ODDebug && ODDebugCursor
  2139.     //PRINT("MouseEnter %d %d\n",eventInfo->where.IntX(),eventInfo->where.IntY());
  2140. #endif
  2141.             handled = kODTrue;
  2142.             break;
  2143.         case kODEvtMouseWithin:
  2144. #if ODDebug && ODDebugCursor
  2145.     PRINT("MouseWithin Frame X: %d Y: %d\n",eventInfo->where.IntX(),eventInfo->where.IntY());
  2146. #endif
  2147.     //$$$$$ Get the point back to window coordinates. Can't we just look at the original event?
  2148.  
  2149.             ODPoint xForm = eventInfo->where;
  2150.             { TempODTransform wcTform = facet->AcquireWindowFrameTransform(ev, kODNULL);
  2151.               wcTform->TransformPoint(ev, &xForm);
  2152.             }
  2153.  
  2154. #if ODDebug && ODDebugCursor
  2155.     PRINT("MouseWithin Window X: %d Y: %d\n",xForm.IntX(),xForm.IntY());
  2156. #endif
  2157.  
  2158.             somSelf->AdjustCursor(ev, &xForm);
  2159.             
  2160.             handled = kODTrue;
  2161.             break;
  2162.         case kODEvtMouseLeave:
  2163. #if ODDebug && ODDebugCursor
  2164.     PRINT("MouseLeave\n");
  2165. #endif
  2166.             SetCursor(&(ODQDGlobals.arrow));
  2167.             handled = kODTrue;
  2168.             break;
  2169.         default:
  2170.             return kODFalse;
  2171.     }
  2172.     return handled;
  2173.  
  2174.     SOM_CATCH_ALL
  2175.     SOM_ENDTRY
  2176.     return kODFalse;
  2177. }
  2178.  
  2179. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextHandleMouseDown(AppleTestDrag_DragText *somSelf, Environment *ev,
  2180.         ODFacet* facet,
  2181.         ODPoint* where,
  2182.         ODEventData* event)
  2183. {
  2184.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2185.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleMouseDown");
  2186.  
  2187.     SOM_TRY
  2188.  
  2189.     GrafPtr            curPort;
  2190.     Point            thePoint;
  2191.     Point             xForm, localPoint;
  2192.     ODShape*        clipShape = kODNULL;
  2193.     RgnHandle        saveClip, clipRgn;
  2194.     ODBoolean        bgClick = (event->what==kODEvtBGMouseDown);
  2195.     ODBoolean        result = !bgClick;        // default result is false if bg
  2196.     
  2197.     if ( !facet )
  2198.     {
  2199.         somPrintf("DragText %d: HandleMouseDown: facet is null\n", somSelf->GetID(ev));
  2200.         return kODFalse;
  2201.     }
  2202.     
  2203.     ODWindow* window = facet->GetWindow(ev);
  2204.     if (!bgClick && !window->IsActive(ev))
  2205.         SelectWindow(window->GetPlatformWindow(ev));
  2206.     else 
  2207.     {
  2208.         GetPort(&curPort);
  2209.         SetPort(_fDocument->theWindow);
  2210.         thePoint = event->where;
  2211.         GlobalToLocal(&thePoint);
  2212.  
  2213.         xForm = GetAggregateTranslation(ev, facet).AsQDPoint();
  2214.         localPoint = where->AsQDPoint();
  2215.         SubPt(xForm, &localPoint);
  2216.         
  2217.         clipShape = facet->AcquireClipShape(ev, kODNULL);
  2218.         clipRgn = clipShape->CopyQDRegion(ev);
  2219.         saveClip = ODNewRgn();
  2220.         GetClip(saveClip);
  2221.         
  2222.         OffsetRgn(clipRgn, xForm.h, xForm.v);
  2223.         SetClip(clipRgn);
  2224.  
  2225. #ifdef SHOW_SCROLL_BARS
  2226.         // <eeh> better to put this on the stack, but I can't get it to work
  2227.         ControlActionUPP    scrollProcUPP = NewControlActionProc( ScrollProc ) ;
  2228.         ControlHandle        theControl;
  2229.         short                thePart;
  2230.  
  2231.         thePart = FindControl(thePoint, _fDocument->theWindow, &theControl);
  2232.         if (thePart && !bgClick) 
  2233.         {
  2234.             if (theControl == _fDocument->vScroll)
  2235.             {
  2236.                 if (thePart == inThumb) 
  2237.                 {
  2238.                     TrackControl(theControl, thePoint, 0L);
  2239.                     AdjustDocumentView(_fDocument);
  2240.                 }
  2241.                 else 
  2242.                 {
  2243.                     TrackControl( theControl, thePoint, scrollProcUPP ) ;
  2244.                 }
  2245.                 somSelf->AdjustScrollBar(ev);
  2246.             }
  2247.         somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  2248.         }
  2249.         else
  2250. #endif
  2251.         if (PtInRgn(thePoint, _fDocument->hiliteRgn))
  2252.         {
  2253.             if ( !_fDocument->frameIsActive )
  2254.                 TEActivate(_fDocument->theTE);
  2255.             if (!somSelf->DoDragText(ev, event)) 
  2256.             {
  2257.                 if( !bgClick ) {
  2258.                     somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  2259.                     TEClick(thePoint, false, _fDocument->theTE);
  2260.                     somSelf->ExpandSelectionAroundLink(ev);
  2261.                 }
  2262.             }
  2263.             else
  2264.             {
  2265.                 if ( !_fDocument->frameIsActive )
  2266.                     TEDeactivate(_fDocument->theTE);
  2267.                 result = kODTrue;        // Handled event even if in bg
  2268.             }
  2269.         }
  2270.         else if (somSelf->PtInDestinationLink(ev, &thePoint))
  2271.         {
  2272.             // If the mouse went down over a destination link, select the entire link
  2273.             // and check for dragging.  If TextEdit isn't active, activate it
  2274.             // so the destination link is hilighted.
  2275.             PRINT("Click over destination link\n");
  2276.             if ( !_fDocument->frameIsActive )
  2277.                 TEActivate(_fDocument->theTE);
  2278.             TESetSelect(_fDstLink.start, _fDstLink.end, _fDocument->theTE);
  2279.             TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  2280.             if ( !somSelf->DoDragText(ev, event) ) {
  2281.                 if( !bgClick )
  2282.                     somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  2283.             } else {
  2284.                 if ( !_fDocument->frameIsActive )
  2285.                     TEDeactivate(_fDocument->theTE);
  2286.                 result = kODTrue;        // Handled event even if in bg
  2287.             }
  2288.  
  2289.         }
  2290.         else if (!bgClick && PtInRect(thePoint, &(**(_fDocument->theTE)).viewRect))
  2291.         {
  2292.             somSelf->ActivateFrame(ev, facet->GetFrame(ev));
  2293.             TEClick(thePoint, (event->modifiers & shiftKey) != 0, _fDocument->theTE);
  2294.             somSelf->ExpandSelectionAroundLink(ev);
  2295.         }
  2296.  
  2297. #ifdef SHOW_SCROLL_BARS
  2298.         DisposeRoutineDescriptor( scrollProcUPP ) ;
  2299. #endif
  2300.  
  2301.         TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  2302.         
  2303.         SetClip(saveClip);
  2304.         
  2305.         if (clipShape) clipShape->Release(ev);
  2306.         ODDisposeHandle((Handle)saveClip);
  2307.         ODDisposeHandle((Handle)clipRgn);
  2308.  
  2309.         SetPort(curPort);
  2310.     }
  2311.  
  2312.     return result;
  2313.  
  2314.     SOM_CATCH_ALL
  2315.     SOM_ENDTRY
  2316.     return kODFalse;
  2317. }
  2318.  
  2319. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextPtInDestinationLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  2320.         Point* thePoint)
  2321. {
  2322.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2323.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","PtInDestinationLink");
  2324.  
  2325.     SOM_TRY
  2326.  
  2327.     ODBoolean result = kODFalse;
  2328.  
  2329.     if ( _fDstLink.link != ((ODLink*) kODNULL) )
  2330.     {
  2331.         RgnHandle destLinkRgn = ODNewRgn();
  2332.         THROW_IF_NULL(destLinkRgn);
  2333.  
  2334.         ODSShort startOffset = _fDstLink.start;
  2335.         do
  2336.         {
  2337.             RgnHandle lineRgn = LineBorderFromRange(&startOffset, _fDstLink.end, _fDocument->theTE);
  2338.             if ( lineRgn != kODNULL )
  2339.             {
  2340.                 UnionRgn(lineRgn, destLinkRgn, destLinkRgn);
  2341.                 ODDisposeHandle((Handle)lineRgn);
  2342.             }
  2343.         }
  2344.         while (startOffset < _fDstLink.end);
  2345.  
  2346.         result = PtInRgn(*thePoint, destLinkRgn);
  2347.         ODDisposeHandle((Handle)destLinkRgn);
  2348.     }
  2349.  
  2350.     return result;
  2351.  
  2352.     SOM_CATCH_ALL
  2353.     SOM_ENDTRY
  2354.     return kODFalse;
  2355. }
  2356.  
  2357. SOM_Scope ODLinkSource*  SOMLINK AppleTestDrag_DragTextCreateLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  2358.         ODByteArray* data)
  2359. {
  2360.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2361.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CreateLink");
  2362.  
  2363. ODUnused(data);
  2364.  
  2365.     SOM_TRY
  2366.  
  2367. #if ODDebug
  2368.     ODISOStr isoStr = (ODISOStr) ODNewPtr(data->_length + 1, kDefaultHeapID);
  2369.     if ( data->_length > 0 )
  2370.         memcpy(isoStr, data->_buffer, data->_length);
  2371.     isoStr[data->_length] = 0;
  2372.  
  2373.     PRINT("DragText: CreateLink: %s, %s\n", isoStr, kDummyLinkSpecData);
  2374.     if ( ODISOStrEqual(isoStr, kDummyLinkSpecData) == kODFalse )
  2375.         WARN("DragText::CreateLink - LinkSpec data corrupted");
  2376.     ODDisposePtr(isoStr);
  2377. #endif
  2378.  
  2379.     TEHandle myTE = _fDocument->theTE;
  2380.     ODULong textSize;
  2381.  
  2382.     if (_fSrcLink.linkSource == (ODLinkSource*) kODNULL) {
  2383.         textSize = _fSrcLink.end-_fSrcLink.start;
  2384.         if (textSize > 0) {
  2385.             _fSrcLink.linkSource = _fDraft->CreateLinkSource(ev, _fPartWrapper);
  2386.             _fSrcLink.change =  _fSession->UniqueUpdateID(ev);
  2387.             somSelf->LinkChanged(ev, &_fSrcLink, _fSrcLink.change);    // default to automatic
  2388.             }
  2389.         }
  2390.  
  2391.     _fSrcLink.linkSource->Acquire(ev);
  2392.     return _fSrcLink.linkSource;
  2393.  
  2394.     SOM_CATCH_ALL
  2395.     SOM_ENDTRY
  2396.     return kODNULL;
  2397. }
  2398.  
  2399. SOM_Scope void  SOMLINK AppleTestDrag_DragTextLinkChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  2400.         AppleTestDrag_DragText_SrceOfLink* srcLink,
  2401.         ODUpdateID updateID)
  2402. {
  2403.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2404.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","LinkChanged");
  2405.  
  2406.     SOM_TRY
  2407.  
  2408. #ifdef LINK_TO_TEXT_ONLY
  2409.     TEHandle myTE = _fDocument->theTE;
  2410.     ODULong textSize;
  2411.     CharsHandle text;
  2412.  
  2413.     if (srcLink->linkSource != (ev, (ODLinkSource*) kODNULL)) {
  2414.         ODLinkKey key;
  2415.  
  2416.         TRY
  2417.             if (srcLink->linkSource->Lock(ev, 0, &key)) {
  2418.                 srcLink->linkSource->Clear(ev, updateID, key);
  2419.                 ODStorageUnit* su = srcLink->linkSource->GetContentStorageUnit(ev, key);
  2420.                 if (su->Exists(ev, kODPropContents, (ODValueType) kODNULL, 0) == kODFalse) {
  2421.                     su->AddProperty(ev, kODPropContents);
  2422.                     }
  2423.                 if (su->Exists(ev, kODPropContents, kODAppleTEXT, 0) == kODFalse) {
  2424.                     su->AddValue(ev, kODAppleTEXT);
  2425.                     }
  2426.                 else {
  2427.                     ODULong valueSize;
  2428.                     su->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  2429.                     valueSize = su->GetSize(ev);
  2430.                     su->DeleteValue(ev, valueSize);
  2431.                     }
  2432.                 su->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  2433.                 textSize = srcLink->end - srcLink->start;
  2434.                 text = (CharsHandle) (*myTE)->hText;
  2435.                 ODLockHandle((ODHandle) text);
  2436.                 StorageUnitSetValue(su, ev, textSize, (ODValue) ((*text) + srcLink->start));
  2437.                 ODUnlockHandle((ODHandle) text);
  2438.                 srcLink->linkSource->ContentUpdated(ev, updateID, key);
  2439.                 srcLink->linkSource->Unlock(ev, key);
  2440.                 _fDirty = kODTrue;
  2441.                 }
  2442.         CATCH(kODErrBrokenLink)
  2443.             PRINT("DragText: ODLinkSource::Lock() returned broken link error\n");
  2444.             somSelf->RemoveSrcLink(ev);
  2445.         CATCH_ALL
  2446.             PRINT("DragText: ODLinkSource::Lock() returned error\n");
  2447.         ENDTRY
  2448.         }
  2449. #else
  2450.     if (srcLink->linkSource != (ev, (ODLinkSource*) kODNULL)) {
  2451.         ODLinkKey key;
  2452.         if (srcLink->linkSource->Lock(ev, 0, &key)) {
  2453.             srcLink->linkSource->Clear(ev, updateID, key);
  2454.  
  2455.             ODStorageUnit* su = srcLink->linkSource->GetContentStorageUnit(ev, key);
  2456.  
  2457. #ifdef BOGUS_CONTENT_FRAME
  2458.             // Testing: Add a content frame property to the content su
  2459.             if ( su->Exists(ev, kODPropContentFrame, kODWeakStorageUnitRef, 0) == kODFalse )
  2460.             {
  2461.                 su->AddProperty(ev, kODPropContentFrame);
  2462.                 su->AddValue(ev, kODWeakStorageUnitRef);
  2463.                 ODStorageUnit* suPseudoFrame = _fDraft->CreateStorageUnit(ev);
  2464.  
  2465.                 ODStorageUnitRef aSURef;
  2466.                 su->GetWeakStorageUnitRef(ev, suPseudoFrame->GetID(ev), aSURef);
  2467.                 StorageUnitSetValue(su, ev, sizeof(ODStorageUnitRef), aSURef);
  2468.                 
  2469.                 suPseudoFrame->Release(ev);
  2470.             }
  2471. #endif
  2472.             TEHandle theTE = _fDocument->theTE;
  2473.             ODSShort selStart = (**(theTE)).selStart;
  2474.             ODSShort selEnd = (**(theTE)).selEnd;
  2475.             
  2476.             // Temporarily change the selection to the link source
  2477.             // don't use TempSelect because of conflict with TSM
  2478. //            TempSelect(srcLink->start, srcLink->end, _fDocument->theTE, _fHasFoci); 
  2479.             (**(theTE)).selStart = srcLink->start;
  2480.             (**(theTE)).selEnd = srcLink->end;
  2481.  
  2482.             somSelf->ExternalizeSelection(ev, su, kODCloneToLink);
  2483.  
  2484.             // Restore the selection
  2485.             // don't use TempSelect because of conflict with TSM
  2486. //            TempSelect(selStart, selEnd, _fDocument->theTE, _fHasFoci);
  2487.             (**(theTE)).selStart = selStart;
  2488.             (**(theTE)).selEnd = selEnd;
  2489.  
  2490.             srcLink->linkSource->ContentUpdated(ev, updateID, key);
  2491.             srcLink->linkSource->Unlock(ev, key);
  2492.             }
  2493.         }
  2494. #endif
  2495.  
  2496.     SOM_CATCH_ALL
  2497.     SOM_ENDTRY
  2498. }
  2499.  
  2500. SOM_Scope void  SOMLINK AppleTestDrag_DragTextLinkUpdated(AppleTestDrag_DragText *somSelf, Environment *ev,
  2501.         ODLink* updatedLink,
  2502.         ODUpdateID change)
  2503. {
  2504.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2505.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","LinkUpdated");
  2506.  
  2507. ODUnused(change);
  2508.  
  2509.     SOM_TRY
  2510.  
  2511.     if ( updatedLink == _fDstLink.link )
  2512.     {
  2513.         ODSShort oldSize = _fDstLink.end - _fDstLink.start;
  2514.         ODSShort selStart = (**(_fDocument->theTE)).selStart;
  2515.         ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  2516.  
  2517. #ifdef SHOW_LINK_BORDERS
  2518.         somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  2519. #endif
  2520.  
  2521.         PRINT("DragText: Updating link destination; old size = %d, \n", oldSize);    
  2522.  
  2523.         // If the frame is not active, must activate TE before inserting
  2524.         // text or the background hilite will not update correctly.
  2525.         ODFrameFacetIterator* fiter = kODNULL;
  2526.         if ( !_fDocument->frameIsActive )
  2527.         {
  2528.             fiter = _fDocument->theFrame->CreateFacetIterator(ev);
  2529.             somSelf->ClipThis(ev, fiter->First(ev));
  2530.             // $$$$$ crc 10/20/94 Not necessary???
  2531.             TEActivate(_fDocument->theTE);
  2532.         }
  2533.  
  2534.         // Select the existing linked text
  2535.         // Unfortunately, TempSelect doesn't seem to clip effectively
  2536.         // TempSelect(_fDstLink.start, _fDstLink.end, _fDocument->theTE, _fDocument->frameIsActive);
  2537.         TESetSelect(_fDstLink.start, _fDstLink.end, _fDocument->theTE);
  2538.         
  2539.         // Replace the selection with the new link content
  2540.         somSelf->IncorporateContentFromLink(ev);
  2541.  
  2542.         ODSShort newSize = _fDstLink.end - _fDstLink.start;
  2543.  
  2544.         PRINT("DragText: Updating link destination; new size = %d\n", newSize);    
  2545.  
  2546.         // Restore the original selection; Doesn't handle overlapping 
  2547.         if ( selStart > _fDstLink.start )
  2548.         {
  2549.             selStart -= (oldSize - newSize);
  2550.         }
  2551.         if ( selEnd > _fDstLink.start )
  2552.         {
  2553.             selEnd -= (oldSize - newSize);
  2554.         }
  2555.         TESetSelect(selStart, selEnd, _fDocument->theTE);
  2556.  
  2557.         // Undo the TEActivate, if needed.
  2558.         if ( !_fDocument->frameIsActive )
  2559.         {
  2560.             // $$$$$ crc 10/20/94 Not necessary???
  2561.             TEDeactivate(_fDocument->theTE);
  2562.             somSelf->ClipToRoot(ev, fiter->First(ev));
  2563.         }
  2564.         ODDeleteObject(fiter);
  2565.     }
  2566.     else
  2567.     {
  2568.         WARN("Unknown link passed to LinkUpdated()!");
  2569.     }
  2570.  
  2571.     SOM_CATCH_ALL
  2572.     SOM_ENDTRY
  2573. }
  2574.  
  2575. SOM_Scope void  SOMLINK AppleTestDrag_DragTextIncorporateContentFromLink(AppleTestDrag_DragText *somSelf, Environment *ev)
  2576. {
  2577.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2578.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","IncorporateContentFromLink");
  2579.  
  2580.     SOM_TRY
  2581.  
  2582.     ODLinkKey key;
  2583.     if (_fDstLink.link->Lock(ev, 0, &key))
  2584.     {
  2585.         TRY
  2586.             ODBoolean successful = kODTrue;
  2587.             ODUpdateID change = _fDstLink.link->GetUpdateID(ev);
  2588.             ODStorageUnit*    su = _fDstLink.link->GetContentStorageUnit(ev, key);
  2589.  
  2590. #ifdef BOGUS_CONTENT_FRAME
  2591.             if ( SUExistsThenFocus(ev, su, kODPropContentFrame, kODWeakStorageUnitRef) )
  2592.             {
  2593.                 ODStorageUnitRef aSURef;
  2594.                 StorageUnitGetValue(su, ev, sizeof(ODStorageUnitRef), &aSURef);
  2595.                 if ( su->IsValidStorageUnitRef(ev, aSURef) )
  2596.                     PRINT("DragText: Content Frame Property found!\n");
  2597.                 else
  2598.                     PRINT("DragText: Content Frame Property missing!\n");
  2599.             }
  2600. #endif
  2601.  
  2602. #ifdef LINK_TO_TEXT_ONLY
  2603.             somSelf->IncorporateTextContent(ev, su, change);
  2604. #else
  2605.             if ( PasteThisKind (ev, su, kODKindTestDrag, _fDstLink.info.kind) )
  2606.             {
  2607.                  somSelf->IncorporateTestDragContent(ev, su, change, kODCloneFromLink);
  2608.             }
  2609.             else if ( PasteThisKind (ev, su, kODApplestxt, _fDstLink.info.kind) )
  2610.             {
  2611.                 somSelf->IncorporateStyledTextContent(ev, su, change);
  2612.             }
  2613.             else if ( PasteThisKind (ev, su, kODApplestyl, _fDstLink.info.kind) )
  2614.             {
  2615.                 somSelf->IncorporateStyledTextContent(ev, su, change);
  2616.             }
  2617.             else if ( PasteThisKind (ev, su, kODAppleTEXT, _fDstLink.info.kind) )
  2618.             {
  2619.                 somSelf->IncorporateTextContent(ev, su, change);
  2620.             }
  2621.             else
  2622.             {
  2623.                 PRINT("DragText %d: Desired kind %s not found in link\n", somSelf->GetID(ev), _fDstLink.info.kind);
  2624.                 successful = kODFalse;
  2625.             }
  2626. #endif
  2627.             if ( successful )
  2628.             {
  2629.                 _fDstLink.info.change = change;
  2630.                 _fDstLink.info.changeTime = _fDstLink.link->GetChangeTime(ev);
  2631.             }
  2632.     
  2633.             _fDstLink.link->Unlock(ev, key);
  2634.     
  2635.         CATCH_ALL
  2636.             if( ErrorCode()==kODErrCannotEstablishLink ) {
  2637.                 ShowErrorAlert(ev, "\pLink could not be created.", ErrorCode(), _fDocument->theFrame);
  2638.                 _fDstLink.link->Unlock(ev, key);
  2639.                 somSelf->RemoveDstLink(ev);
  2640.             } else
  2641.                 _fDstLink.link->Unlock(ev, key);
  2642.  
  2643.         ENDTRY
  2644.  
  2645.     }
  2646.  
  2647.     SOM_CATCH_ALL
  2648.     SOM_ENDTRY
  2649. }
  2650.  
  2651. SOM_Scope void  SOMLINK AppleTestDrag_DragTextExpandSelectionAroundLink(AppleTestDrag_DragText *somSelf, Environment *ev)
  2652. {
  2653.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2654.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ExpandSelectionAroundLink");
  2655.  
  2656.     ODSShort selStart = (**(_fDocument->theTE)).selStart;
  2657.     ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  2658.     ODBoolean expanded = kODFalse;
  2659.     
  2660.     // Make sure the deletion includes the entire link destination
  2661.     if ( (selStart > _fDstLink.start) && (selStart < _fDstLink.end) )
  2662.     {
  2663.         selStart = _fDstLink.start;
  2664.         expanded = kODTrue;
  2665.     }
  2666.     if ( (selEnd > _fDstLink.start) && (selEnd < _fDstLink.end) )
  2667.     {
  2668.         selEnd = _fDstLink.end;
  2669.         expanded = kODTrue;
  2670.     }
  2671.     if ( expanded )
  2672.     {
  2673.         TESetSelect(selStart, selEnd, _fDocument->theTE);
  2674.         TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  2675.     }
  2676. }
  2677.  
  2678. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCheckDstLinkDeletion(AppleTestDrag_DragText *somSelf, Environment *ev,
  2679.         ODSShort start,
  2680.         ODSShort endOld,
  2681.         ODUpdateID change)
  2682. {
  2683.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2684.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CheckDstLinkDeletion");
  2685.  
  2686.     SOM_TRY
  2687.  
  2688.     if ( change != _fDstLink.link->GetUpdateID(ev) )
  2689.     {
  2690.         if ( (start <= _fDstLink.start) && (endOld >= _fDstLink.end) )
  2691.         {
  2692.             // Destination link has been removed.
  2693.             somSelf->RemoveDstLink(ev);
  2694.         }
  2695.     }
  2696.  
  2697.     SOM_CATCH_ALL
  2698.     SOM_ENDTRY
  2699. }
  2700.  
  2701. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAbandonSourceLinks(AppleTestDrag_DragText *somSelf, Environment *ev,
  2702.                         ODSShort     start,
  2703.                         ODSShort     end)
  2704. // Abandon any link sources in the range start..end, without clearing the link.
  2705. {
  2706.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2707.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AbandonSourceLinks");
  2708.  
  2709.     SOM_TRY
  2710.  
  2711.     // Tests copied from AdjustSrcLinks
  2712.     
  2713.     if ( _fSrcLink.linkSource != (ODLinkSource*) kODNULL )
  2714.     {
  2715.         if (((start < _fSrcLink.start) && (end >= _fSrcLink.end)) ||
  2716.             ((start <= _fSrcLink.start) && (end > _fSrcLink.end)) ||
  2717.             ((start == _fSrcLink.start) && (end == _fSrcLink.end)))
  2718.         {
  2719.             // Fall in here if
  2720.             // • The text range start..end included more than the entire link, or
  2721.             // • Exactly the link was deleted
  2722.             // In either case, abandon the link
  2723.     
  2724.             PRINT("DragText: Abandoning source link\n");    
  2725.  
  2726.             _fSrcLink.linkSource->Release(ev);
  2727.             _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  2728.             _fSrcLink.start = -1;
  2729.             _fSrcLink.end = -1;
  2730.             _fSrcLink.change = kODUnknownUpdate;
  2731.         }
  2732.     }
  2733.  
  2734.     SOM_CATCH_ALL
  2735.     SOM_ENDTRY
  2736. }
  2737.  
  2738. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustLinks(AppleTestDrag_DragText *somSelf, Environment *ev,
  2739.                         ODSShort     start,
  2740.                         ODSShort     endOld,
  2741.                         ODSShort     endNew,
  2742.                         ODUpdateID updateID)
  2743. // Adjust source and destination links following a change to the text edit record
  2744. {
  2745.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2746.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustLinks");
  2747.  
  2748.     SOM_TRY
  2749.  
  2750.     somSelf->AdjustDstLinks(ev, start, endOld, endNew, updateID);
  2751.     somSelf->AdjustSrcLinks(ev, start, endOld, endNew, updateID);
  2752.  
  2753.     SOM_CATCH_ALL
  2754.     SOM_ENDTRY
  2755. }
  2756.  
  2757. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustDstLinks(AppleTestDrag_DragText *somSelf, Environment *ev,
  2758.         ODSShort start,
  2759.         ODSShort endOld,
  2760.         ODSShort endNew,
  2761.         ODUpdateID change)
  2762. {
  2763.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2764.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustDstLinks");
  2765.  
  2766.     SOM_TRY
  2767.  
  2768.     // Adjust destination links following a change to the text edit record
  2769.  
  2770.     if ( _fDstLink.link && (endOld > start) )
  2771.     {
  2772.         // Some content was replaced, so see if the destination link
  2773.         // was deleted
  2774.         somSelf->CheckDstLinkDeletion (ev, start, endOld, change);
  2775.     }
  2776.         
  2777.     if ( _fDstLink.link )
  2778.     {
  2779.         if ( (start == _fDstLink.start) && (endOld == _fDstLink.end) )
  2780.         {
  2781.             // Destination link has been updated
  2782.             _fDstLink.end = endNew;
  2783.         }
  2784.         else if ( start >= _fDstLink.end )
  2785.         {
  2786.             // No change to destination link
  2787.         }
  2788.         else
  2789.         {
  2790.             // Must be entirely before the destination link
  2791.             _fDstLink.start += (endNew - endOld);
  2792.             _fDstLink.end += (endNew - endOld);
  2793.         }
  2794.     }
  2795.  
  2796.     SOM_CATCH_ALL
  2797.     SOM_ENDTRY
  2798. }
  2799.  
  2800. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustSrcLinks(AppleTestDrag_DragText *somSelf, Environment *ev,
  2801.         ODSShort start,
  2802.         ODSShort endOld,
  2803.         ODSShort endNew,
  2804.         ODUpdateID change)
  2805. {
  2806.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2807.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustSrcLinks");
  2808.  
  2809.     SOM_TRY
  2810.  
  2811.     // Adjust source link offsets following a change to the text edit record
  2812.     // Text in the range start..endOld was replaced by the range start..endNew.
  2813.     // Note that this routine calls ContentUpdated(); this can cause the LinkUpdated
  2814.     // method of this object to be called, which calls this method.
  2815.     // On entry, _fSrcLink contains old offsets.  On exit, _fSrcLink contains new offsets.
  2816.  
  2817.     if ((start >= _fSrcLink.end) && (endOld > _fSrcLink.end)) {
  2818.         // Source link not affected
  2819.         // Note that we don't fall in here if text is inserted immediately after
  2820.         // the link, that is, if start == endOld == _fSrcLink.end
  2821.         return;
  2822.         }
  2823.     if (((start < _fSrcLink.start) && (endOld >= _fSrcLink.end)) ||
  2824.         ((start <= _fSrcLink.start) && (endOld > _fSrcLink.end)) ||
  2825.         ((endNew == start) && (start == _fSrcLink.start) && (endOld == _fSrcLink.end))) {
  2826.         // Fall in here if
  2827.         // • The text range start..endOld included more than the entire link, or
  2828.         // • Exactly the link was deleted with no replacement
  2829.         // In either case, delete the link
  2830.         somSelf->RemoveSrcLink(ev);
  2831.         return;
  2832.         }
  2833.     if ((endOld <= _fSrcLink.start) && (start < _fSrcLink.start)) {
  2834.         // Changes occurred ahead of the source link, so just move it accordingly
  2835.         // (Treat the case of an insertion point at the beginning of the link as
  2836.         // inserting into the link; handled below).
  2837.         _fSrcLink.start += (endNew - endOld);
  2838.         _fSrcLink.end += (endNew - endOld);
  2839.         return;
  2840.         }
  2841.     if (start == endOld) {
  2842.         // Additions occurred at the insertion point after the last character in
  2843.         // the link
  2844.         _fSrcLink.end += (endNew - endOld);
  2845.         }
  2846.     else if (start < _fSrcLink.start) {
  2847.         // Only that portion of link unaffected remains within the link
  2848.         _fSrcLink.start = start + endNew; 
  2849.         _fSrcLink.end += (endNew - endOld);
  2850.         }
  2851.     else if (endOld > _fSrcLink.end) {
  2852.         // Only that portion of link unaffected remains within the link
  2853.         // The replaced text starts in the selection and continues beyond
  2854.         _fSrcLink.end = start;
  2855.         }
  2856.     else {
  2857.         // Replaced text lies entirely within the link, so all new text
  2858.         // is included in the link
  2859.         _fSrcLink.end += (endNew - endOld);
  2860.         }
  2861.  
  2862.     _fSrcLink.change = change;
  2863.     if ( _fSrcLink.linkSource != (ODLinkSource*) kODNULL )
  2864.         if ( _fSrcLink.linkSource->IsAutoUpdate(ev) )
  2865.             somSelf->LinkChanged(ev, &_fSrcLink, change);
  2866.     somSelf->RemoveLinkSpec(ev);
  2867.  
  2868.     SOM_CATCH_ALL
  2869.     SOM_ENDTRY
  2870. }
  2871.  
  2872. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRemoveDstLink(AppleTestDrag_DragText *somSelf, Environment *ev)
  2873. {
  2874.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2875.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RemoveDstLink");
  2876.  
  2877.     SOM_TRY
  2878.  
  2879. #ifdef SHOW_LINK_BORDERS
  2880.     somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  2881. #endif
  2882.  
  2883.     if (_fDstLink.link != ((ODLink*) kODNULL))
  2884.     {
  2885.         if ( _fDstLink.info.autoUpdate )
  2886.             _fDstLink.link->UnregisterDependent(ev, _fPartWrapper);
  2887.         _fDstLink.link->Release(ev);
  2888.     }
  2889.     _fDstLink.link = (ODLink*) kODNULL;
  2890.     _fDstLink.start = -1;
  2891.     _fDstLink.end = -1;
  2892.     _fDstLink.info.kind = (ODType) kODNULL;
  2893.     _fDirty = kODTrue;
  2894.     _fDraft->SetChangedFromPrev(ev);
  2895.  
  2896.     SOM_CATCH_ALL
  2897.     SOM_ENDTRY
  2898. }
  2899.  
  2900. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRemoveSrcLink(AppleTestDrag_DragText *somSelf, Environment *ev)
  2901. {
  2902.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2903.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RemoveSrcLink");
  2904.  
  2905.     SOM_TRY
  2906.  
  2907.     ODLinkSource* linkSource = _fSrcLink.linkSource;
  2908.     
  2909. #ifdef SHOW_LINK_BORDERS
  2910.     somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  2911. #endif
  2912.  
  2913.     AppleTestDrag_DragText_SrceOfLink tempSrcLink = _fSrcLink;
  2914.  
  2915.     // Null the source link prior to calling ContentUpdated() to prevent
  2916.     // indefinite recursion.
  2917.     _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  2918.  
  2919.     if ( tempSrcLink.linkSource )
  2920.     {
  2921.         // Update the link with empty content.
  2922.         // Calling ODLinkSource::Clear() is not sufficient, because an
  2923.         // empty value is not valid; a valid Test Drag value is a reference
  2924.         // to a storage unit, and a zero-length styl record isn't valid.
  2925.         tempSrcLink.start = 0;
  2926.         tempSrcLink.end = 0;
  2927.         TRY
  2928.             // $$$$$ Need to use the change id associated with the operation that
  2929.             //  caused the source link to be removed
  2930.             somSelf->LinkChanged(ev, &tempSrcLink, _fSession->UniqueUpdateID(ev));
  2931.         CATCH_ALL
  2932.         ENDTRY
  2933.         tempSrcLink.linkSource->SetSourcePart(ev, kODNULL);
  2934.         tempSrcLink.linkSource->Release(ev);
  2935.     }
  2936.  
  2937.     somSelf->RemoveLinkSpec(ev);
  2938.     _fSrcLink.start = -1;
  2939.     _fSrcLink.end = -1;
  2940.     _fSrcLink.change = kODUnknownUpdate;
  2941.  
  2942.     SOM_CATCH_ALL
  2943.     SOM_ENDTRY
  2944. }
  2945.  
  2946. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRemoveLinkSpec(AppleTestDrag_DragText *somSelf, Environment *ev)
  2947. {
  2948.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2949.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RemoveLinkSpec");
  2950.  
  2951.     SOM_TRY
  2952.  
  2953.     // $$$$$ Really should pass in the correct frame
  2954.     ODFrame* frame = (ODFrame*) _fDisplayFrames->First();
  2955.     
  2956.     if ( frame != (ODFrame*) kODNULL )
  2957.     {
  2958.         ODArbitrator* arbitrator = _fSession->GetArbitrator(ev);
  2959.         ODFrame* clipboardFocus = arbitrator->AcquireFocusOwner(ev, _fClipboardFocus);
  2960.         if ( (frame == clipboardFocus) || 
  2961.              (arbitrator->RequestFocus(ev, _fClipboardFocus, frame)) )
  2962.         {
  2963.             // Remove the link spec from the clipboard
  2964.             ODClipboard* clipboard = _fSession->GetClipboard(ev);
  2965.             if ( _fClipboardUpdateID == clipboard->GetUpdateID(ev) )
  2966.             {
  2967.                 TRY
  2968.                     ODStorageUnit* clipRootSU = clipboard->GetContentStorageUnit(ev);
  2969.                     if ( clipRootSU->Exists(ev, kODPropLinkSpec, (ODValueType) kODNULL, 0) != kODFalse )
  2970.                     {
  2971.                         clipRootSU->Focus(ev, kODPropLinkSpec, kODPosUndefined, (ODValueType) kODNULL, 0, kODPosUndefined);
  2972.                         clipRootSU->Remove(ev);
  2973.                     }
  2974.                 CATCH_ALL
  2975.                 ENDTRY
  2976.             }
  2977.         }
  2978.         ODReleaseObject(ev, clipboardFocus);
  2979.     }
  2980.  
  2981.     SOM_CATCH_ALL
  2982.     SOM_ENDTRY
  2983. }
  2984.  
  2985. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextHandleMouseDownInBorder(AppleTestDrag_DragText *somSelf, Environment *ev,
  2986.         ODFacet* container,
  2987.         ODFacet* facet,
  2988.         ODPoint* where,
  2989.         ODEventData* event)
  2990. {
  2991.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  2992.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleMouseDownInBorder");
  2993.  
  2994. ODUnused(container);
  2995. ODUnused(facet);
  2996. ODUnused(where);
  2997. ODUnused(event);
  2998.     SysBeep(2);
  2999.     return kODFalse;
  3000. }
  3001.  
  3002. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextHandleKeyDown(AppleTestDrag_DragText *somSelf, Environment *ev,
  3003.         ODFrame* focusFrame,
  3004.         ODEventData* event)
  3005. {
  3006.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3007.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleKeyDown");
  3008.  
  3009.     SOM_TRY
  3010.  
  3011.     ODBoolean handled = kODFalse;
  3012.     
  3013. #ifdef SHOW_LINK_BORDERS
  3014.     somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  3015. #endif
  3016.  
  3017.     if (/*!ParentPrtRCR::HandleKeyDown(focusFrame, event) &&*/ focusFrame)
  3018.     {
  3019.         if (event->modifiers & (0xFFFFFFFF & cmdKey))    // was cmd key down?
  3020.         {
  3021.             ODULong mcValue = MenuKey((short)(event->message & charCodeMask));
  3022.             ODCommandID command = _fMenuBar->GetCommand(ev, HiWord(mcValue),LoWord(mcValue));
  3023.             handled = somSelf->DoCommand(ev, focusFrame,command);
  3024.         }
  3025.         else if ( focusFrame->GetLinkStatus(ev) == kODInLinkDestination )
  3026.         {
  3027.             if ( !focusFrame->EditInLink(ev) )
  3028.                 // Put up an alert;
  3029.                 WARN("DragText: The destination of a link cannot be edited.");
  3030.         }
  3031.         else
  3032.         {
  3033.             ODSShort delStart;
  3034.             ODSShort delEnd;
  3035.             ODSShort newEnd;
  3036.             char key = (char) (event->message) & charCodeMask;
  3037.             
  3038.             delStart = (**(_fDocument->theTE)).selStart;
  3039.             delEnd = (**(_fDocument->theTE)).selEnd;
  3040.             
  3041.             ODFrameFacetIterator* fiter = focusFrame->CreateFacetIterator(ev);
  3042.             /* $$$$$ */ SetPort(fiter->First(ev)->GetCanvas(ev)->GetQDPort(ev));
  3043.             somSelf->ClipThis(ev, fiter->First(ev));
  3044.  
  3045.             if ( key == 8 /* Delete key */ )
  3046.             {
  3047.                 PRINT("Delete key\n");
  3048.                 ODSShort selStart = (**(_fDocument->theTE)).selStart;
  3049.                 ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  3050.  
  3051.                 if ( (selStart == selEnd) && (selStart == _fDstLink.end) )                
  3052.                     TESetSelect(_fDstLink.start, _fDstLink.end, _fDocument->theTE);
  3053.             }
  3054.  
  3055.             TEKey(key, _fDocument->theTE);
  3056.             somSelf->AdjustScrollBar(ev);
  3057.             TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  3058.  
  3059.             somSelf->ClipToRoot(ev, fiter->First(ev));
  3060.             handled = kODTrue;
  3061.             ODDeleteObject(fiter);
  3062.             
  3063.             if ((key == kRightArrow) || (key == kLeftArrow) || (key == kUpArrow) || (key == kDownArrow))
  3064.             {
  3065.                 somSelf->ExpandSelectionAroundLink(ev);
  3066.             }
  3067.             else
  3068.             {
  3069.                 newEnd = (**(_fDocument->theTE)).selEnd;
  3070.                 if ((delEnd > delStart) || (newEnd != delEnd))
  3071.                 {
  3072.                     // Some change occurred
  3073.                     if ((delEnd == delStart) && (newEnd < delStart))
  3074.                     {
  3075.                         // Special case for delete key
  3076.                         delStart = newEnd;
  3077.                     }
  3078.                     ODUpdateID updateID = _fSession->UniqueUpdateID(ev);
  3079.                     somSelf->AdjustLinks(ev, delStart, delEnd, newEnd, updateID);
  3080.                     focusFrame->ContentUpdated(ev, updateID);
  3081.                 }
  3082.                 _fDirty = kODTrue;
  3083.                 _fDraft->SetChangedFromPrev(ev);
  3084.             }
  3085.         }
  3086.     }
  3087.     return (handled);
  3088.  
  3089.     SOM_CATCH_ALL
  3090.     SOM_ENDTRY
  3091.     return kODFalse;
  3092. }
  3093.  
  3094. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoCut(AppleTestDrag_DragText *somSelf, Environment *ev,
  3095.         ODFrame* frame)
  3096. {
  3097.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3098.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoCut");
  3099.  
  3100.     SOM_TRY
  3101.  
  3102.     if ( frame->GetLinkStatus(ev) == kODInLinkDestination )
  3103.     {
  3104.         if ( !frame->EditInLink(ev) )
  3105.             // Put up an alert;
  3106.             WARN("DragText: The destination of a link cannot be edited.");
  3107.         return kODTrue;
  3108.     }
  3109.  
  3110.     ODFrame* clipboardFocus = _fSession->GetArbitrator(ev)->AcquireFocusOwner(ev, _fClipboardFocus);
  3111.     WASSERTM((frame == clipboardFocus), "DragText::DoCut - Clipboard focus not owned");
  3112.     ODReleaseObject(ev, clipboardFocus);
  3113.  
  3114.     ODClipboard* clipboard = _fSession->GetClipboard(ev);
  3115.  
  3116.     ODStorageUnit* clipBSU = kODNULL;
  3117.  
  3118.     ODSShort delStart = (**(_fDocument->theTE)).selStart;
  3119.     ODSShort delEnd = (**(_fDocument->theTE)).selEnd;
  3120.  
  3121.     ODPtr text;
  3122.     ODHandle style;
  3123.     
  3124.     TRY
  3125.         clipboard->Clear(ev);
  3126.         clipBSU = clipboard->GetContentStorageUnit(ev);
  3127.         somSelf->ExternalizeSelection(ev, clipBSU, kODCloneCut);
  3128.         _fClipboardUpdateID = clipboard->GetUpdateID(ev);
  3129.     CATCH_ALL
  3130.         WARN("Cut to clipboard failed");
  3131.     ENDTRY
  3132.         
  3133.     text = ODNewPtr(delEnd-delStart, kDefaultHeapID);
  3134.     ODBlockMove((ODPtr)((*(char **)((**(_fDocument->theTE)).hText)) + (**(_fDocument->theTE)).selStart),text,delEnd-delStart);
  3135.     
  3136.     TEDelete(_fDocument->theTE);
  3137.  
  3138.     ODUpdateID updateID = _fSession->UniqueUpdateID(ev);
  3139.     somSelf->AdjustLinks(ev, delStart, delEnd, delStart, updateID);
  3140.     frame->ContentUpdated(ev, updateID);
  3141.     
  3142.     style = (ODHandle)TEGetStyleScrapHandle(_fDocument->theTE);
  3143.  
  3144.     somSelf->PushUndoAction(ev, frame,kODCommandCut,delStart,delEnd,text,style,
  3145.                             "\pUndo Cut", "\pRedo Cut");
  3146.  
  3147.     _fDirty = kODTrue;
  3148.     _fDraft->SetChangedFromPrev(ev);
  3149.  
  3150.     return kODTrue;
  3151.  
  3152.     SOM_CATCH_ALL
  3153.     SOM_ENDTRY
  3154.     return kODTrue;
  3155. }
  3156.  
  3157. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoCut(AppleTestDrag_DragText *somSelf, Environment *ev,
  3158.         UndoInfo* action,
  3159.         ODUShort selector)
  3160. {
  3161.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3162.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoCut");
  3163.  
  3164.      ODUnused(action);
  3165.      ODUnused(selector);
  3166. }
  3167.  
  3168. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAddLinkSpecToSU(AppleTestDrag_DragText *somSelf, Environment *ev,
  3169.         ODStorageUnit* su)
  3170. {
  3171.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3172.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AddLinkSpecToSU");
  3173.  
  3174.     SOM_TRY
  3175.  
  3176. #ifdef SUPPORT_TESTDRAG_KIND
  3177.     // Add a link spec to the storage unit if we don't already have a source link
  3178.     // (or the source link is selected exactly),
  3179.     // and as long as the selection does not include a link destination.
  3180.     // Also, don't add a link spec if draft permissions don't allow writing.
  3181.  
  3182.     ODBoolean makeLinkSpec = kODFalse;
  3183.     
  3184.     if (HAS_WRITE_ACCESS(_fDraft->GetPermissions(ev)))
  3185.     {
  3186.         if (_fSrcLink.linkSource == (ODLinkSource*) kODNULL)
  3187.         {
  3188.             if (_fDstLink.link == (ODLink*) kODNULL)
  3189.             {
  3190.                 makeLinkSpec = kODTrue;
  3191.             }
  3192.             else
  3193.             {
  3194.                 ODSShort selStart = (**(_fDocument->theTE)).selStart;
  3195.                 ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  3196.                 makeLinkSpec = !RangesOverlap(selStart,selEnd,_fDstLink.start,_fDstLink.end);
  3197.             }
  3198.         }
  3199.         else
  3200.         {
  3201.             ODSShort selStart = (**(_fDocument->theTE)).selStart;
  3202.             ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  3203.             makeLinkSpec = (selStart==_fSrcLink.start) && (selEnd==_fSrcLink.end);
  3204.         }
  3205.     }
  3206.  
  3207.     if (makeLinkSpec)
  3208.     {
  3209.         ODByteArray data = CreateByteArrayStruct(kDummyLinkSpecData, ODISOStrLength(kDummyLinkSpecData));        
  3210.         ODLinkSpec* ls = _fDraft->CreateLinkSpec(ev,
  3211.                                     _fPartWrapper,
  3212.                                     &data);
  3213.         DisposeByteArrayStruct(data);
  3214.         su->AddProperty(ev, kODPropLinkSpec);
  3215.         ls->WriteLinkSpec(ev, su);
  3216.         ODDeleteObject(ls);
  3217.         
  3218.         // Save away the range for the potential link
  3219.         if (_fSrcLink.linkSource == (ODLinkSource*) kODNULL)
  3220.         {
  3221.             _fSrcLink.start = (**(_fDocument->theTE)).selStart;
  3222.             _fSrcLink.end = (**(_fDocument->theTE)).selEnd;
  3223.         }
  3224.     }
  3225. #endif
  3226.  
  3227.     SOM_CATCH_ALL
  3228.     SOM_ENDTRY
  3229. }
  3230.  
  3231. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoCopy(AppleTestDrag_DragText *somSelf, Environment *ev,
  3232.         ODFrame* frame)
  3233. {
  3234.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3235.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoCopy");
  3236.  
  3237.     ODUnused(frame);
  3238.     
  3239.     SOM_TRY
  3240.  
  3241.     ODFrame* clipboardFocus = _fSession->GetArbitrator(ev)->AcquireFocusOwner(ev, _fClipboardFocus);
  3242.     WASSERTM((frame == clipboardFocus), "DragText::DoCopy - Clipboard focus not owned");
  3243.     ODReleaseObject(ev, clipboardFocus);
  3244.  
  3245.     ODClipboard* clipboard = _fSession->GetClipboard(ev);
  3246.  
  3247.     TRY
  3248.         ODStorageUnit* clipBSU = kODNULL;
  3249.  
  3250.         clipboard->Clear(ev);
  3251.         clipBSU = clipboard->GetContentStorageUnit(ev);
  3252.         
  3253.         somSelf->ExternalizeSelection(ev, clipBSU, kODCloneCopy);
  3254.         _fClipboardUpdateID = clipboard->GetUpdateID(ev);
  3255.  
  3256.         somSelf->AddLinkSpecToSU(ev, clipBSU);
  3257.         
  3258. #ifdef TRYOUT_PROMISE
  3259.         char buffer[] = "abcd";
  3260.         clipBSU->Focus(ev, kODPropContents, kODPosUndefined, kODNULL, 0, kODPosAll);
  3261.         StorageUnitSetPromiseValue(clipBSU, ev, "SomePromiseValue", 0, 4, buffer, _fPartWrapper);
  3262. #endif
  3263.  
  3264.     CATCH_ALL
  3265.         WARN("Copy to clipboard failed");
  3266.     ENDTRY
  3267.  
  3268.     return kODTrue;
  3269.  
  3270.     SOM_CATCH_ALL
  3271.     SOM_ENDTRY
  3272.     return kODTrue;
  3273. }
  3274.  
  3275. SOM_Scope void  SOMLINK AppleTestDrag_DragTextIncorporateTestDragContent(AppleTestDrag_DragText *somSelf, Environment *ev,
  3276.         ODStorageUnit* su,
  3277.         ODUpdateID change,
  3278.         ODCloneKind cloneKind)
  3279. {
  3280.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3281.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","IncorporateTestDragContent");
  3282.  
  3283.     SOM_TRY
  3284.     
  3285.     ODSShort            delStart = (**(_fDocument->theTE)).selStart;
  3286.     ODSShort            delEnd = (**(_fDocument->theTE)).selEnd;
  3287.     ODSShort            newEnd;
  3288.     ODValue                text = kODNULL;
  3289.     ODULong                size = 0;
  3290.     ODULong                stsize = 0;
  3291.     StScrpHandle        style = kODNULL;
  3292.     ODStorageUnit*        testDragSU = kODNULL;
  3293.     ODStorageUnitRef    aSURef;
  3294.     ODDraft*            fromDraft = su->GetDraft(ev);
  3295.  
  3296.     ODID                newLinkID = kODNULLID;
  3297.     ODSShort            newLinkStart;
  3298.     ODSShort            newLinkEnd;
  3299.     ODLinkInfo            newLinkInfo;
  3300.  
  3301.     ODID                newLinkSourceID = kODNULLID;
  3302.     ODSShort            newLinkSourceStart;
  3303.     ODSShort            newLinkSourceEnd;
  3304.     ODUpdateID            newLinkSourceChange;
  3305.  
  3306.     ODDraftKey draftKey = fromDraft->BeginClone(ev, _fDraft, (ODFrame*) _fDisplayFrames->First(), cloneKind);
  3307.     TRY
  3308.         su->Focus(ev, kODPropContents, kODPosUndefined, kODKindTestDrag, (ODValueIndex)0, kODPosUndefined);
  3309.         StorageUnitGetValue(su, ev, sizeof(ODStorageUnitRef),&aSURef);
  3310.         testDragSU = fromDraft->AcquireStorageUnit(ev, su->GetIDFromStorageUnitRef(ev, aSURef));
  3311.     
  3312.         testDragSU->Focus(ev, kDragTextPropText, kODPosUndefined, 0, 1, kODPosFirstSib);
  3313.         size = testDragSU->GetSize(ev);
  3314.         text = (ODValue) ODNewPtr(size, kDefaultHeapID);
  3315.         
  3316.         testDragSU->SetOffset(ev, 0);
  3317.         StorageUnitGetValue(testDragSU, ev, size, text);
  3318.         
  3319.         testDragSU->Focus(ev, kDragTextPropStyl, kODPosUndefined, kODApplestyl, (ODValueIndex)0, kODPosUndefined);
  3320.         stsize = testDragSU->GetSize(ev);
  3321.         style = (StScrpHandle)ODNewHandle(stsize);
  3322.         
  3323.         ODLockHandle((ODHandle) style);
  3324.         testDragSU->SetOffset(ev, 0);
  3325.         StorageUnitGetValue(testDragSU, ev, stsize,(ODValue)(*style));
  3326.         ODUnlockHandle((ODHandle) style);
  3327.     
  3328.         TEDelete(_fDocument->theTE);
  3329.         TEStyleInsert((void*)text, size, style, _fDocument->theTE);
  3330.         
  3331.         if ( _fSrcLink.linkSource == (ODLinkSource*) kODNULL )
  3332.         {
  3333.             if ( testDragSU->Exists(ev, kDragTextPropSourceLink, kDragTextValue, kODPosAll) )
  3334.             {
  3335.                 testDragSU->Focus(ev, kDragTextPropSourceLink, kODPosUndefined, kDragTextValue, (ODValueIndex)0, kODPosUndefined);
  3336.                 if ( testDragSU->GetSize(ev) > 0 ) 
  3337.                 {
  3338.                     StorageUnitGetValue(testDragSU, ev, sizeof(ODStorageUnitRef), &aSURef);
  3339.                     if ( testDragSU->IsValidStorageUnitRef(ev, aSURef) )
  3340.                     {
  3341.                         newLinkSourceID = fromDraft->Clone(ev, draftKey, testDragSU->GetIDFromStorageUnitRef(ev, aSURef), 0, 0);
  3342.                         StorageUnitGetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &newLinkSourceStart);
  3343.                         StorageUnitGetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &newLinkSourceEnd);
  3344.                         StorageUnitGetValue(testDragSU, ev, sizeof(ODULong), (ODValue) &newLinkSourceChange);
  3345.                     }
  3346.                 }
  3347.             }
  3348.         }
  3349.  
  3350.         if ( _fDstLink.link == (ODLink*) kODNULL )
  3351.         {
  3352.             if ( testDragSU->Exists(ev, kDragTextPropDestLink, kDragTextValue, kODPosAll) )
  3353.             {
  3354.                 testDragSU->Focus(ev, kDragTextPropDestLink, kODPosUndefined, kDragTextValue, (ODValueIndex)0, kODPosUndefined);
  3355.                 if ( testDragSU->GetSize(ev) > 0 ) 
  3356.                 {
  3357.                     StorageUnitGetValue(testDragSU, ev, sizeof(ODStorageUnitRef), &aSURef);
  3358.                     if ( testDragSU->IsValidStorageUnitRef(ev, aSURef) )
  3359.                     {
  3360.                         newLinkID = fromDraft->Clone(ev, draftKey, testDragSU->GetIDFromStorageUnitRef(ev, aSURef), 0, 0);
  3361.                         StorageUnitGetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &newLinkStart);
  3362.                         StorageUnitGetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &newLinkEnd);
  3363.                         StorageUnitGetValue(testDragSU, ev, sizeof(ODLinkInfo), (ODValue) &newLinkInfo);
  3364.                         ODType kind = (ODType) ODNewPtr((ODULong) newLinkInfo.kind, kDefaultHeapID);
  3365.                         StorageUnitGetValue(testDragSU, ev, (ODULong) newLinkInfo.kind, (ODValue) kind);
  3366.                         newLinkInfo.kind = kind;
  3367.                     }
  3368.                 }
  3369.             }
  3370.         }
  3371.  
  3372.         testDragSU->Release(ev);
  3373.  
  3374.     CATCH_ALL
  3375.         ODReleaseObject(ev, testDragSU);
  3376.         fromDraft->AbortClone(ev, draftKey);
  3377.         RERAISE;
  3378.     ENDTRY
  3379.     fromDraft->EndClone(ev, draftKey);
  3380.  
  3381.     newEnd = (**(_fDocument->theTE)).selEnd;
  3382.     somSelf->AdjustLinks(ev, delStart, delEnd, newEnd, change);
  3383.  
  3384.     if ( _fSrcLink.linkSource == (ODLinkSource*) kODNULL )
  3385.     {
  3386.         if ( _fDraft->IsValidID(ev, newLinkSourceID) )
  3387.         {
  3388.             TRY
  3389.                 _fSrcLink.linkSource = _fDraft->AcquireLinkSource(ev, newLinkSourceID);
  3390.                 _fSrcLink.start = newLinkSourceStart;
  3391.                 _fSrcLink.end = newLinkSourceEnd;
  3392.                 _fSrcLink.change = newLinkSourceChange;
  3393.                 _fSrcLink.start += delStart;
  3394.                 _fSrcLink.end += delStart;
  3395.  
  3396.                 // Change the link source to reference this part's storage unit
  3397.                 _fSrcLink.linkSource->SetSourcePart(ev, somSelf->GetStorageUnit(ev));
  3398.  
  3399.                 // Now notify destinations; necessary if this source link was cut
  3400.                 //  but some destinations were not.
  3401.                 //     12.22.94 crc: I don't remember why this is necessary.
  3402.                 //  For now I'll assume it is only necessary for automatic links.
  3403.                 if ( _fSrcLink.change != _fSrcLink.linkSource->GetUpdateID(ev) )
  3404.                     if ( _fSrcLink.linkSource->IsAutoUpdate(ev) )
  3405.                         somSelf->LinkChanged(ev, &_fSrcLink, _fSrcLink.change);
  3406.             CATCH_ALL
  3407.                 _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  3408.                 _fSrcLink.start = -1;
  3409.                 _fSrcLink.end = -1;
  3410.                 _fSrcLink.change = kODUnknownUpdate;
  3411.             ENDTRY
  3412.         }
  3413.     }
  3414.  
  3415.     if ( _fDstLink.link == (ODLink*) kODNULL )
  3416.     {
  3417.         if ( _fDraft->IsValidID(ev, newLinkID) )
  3418.         {
  3419.             TRY
  3420.                 _fDstLink.link = _fDraft->AcquireLink(ev, newLinkID, kODNULL);
  3421.                 _fDstLink.start = newLinkStart;
  3422.                 _fDstLink.end = newLinkEnd;
  3423.                 _fDstLink.start += delStart;
  3424.                 _fDstLink.end += delStart;
  3425.     
  3426.                 ODDisposePtr(_fDstLink.info.kind);
  3427.                 _fDstLink.info = newLinkInfo;
  3428.     
  3429.                 // Register as a dependent of the link if updates are automatic
  3430.                 if ( _fDstLink.info.autoUpdate )
  3431.                     _fDstLink.link->RegisterDependent(ev, _fPartWrapper, _fDstLink.info.change);
  3432.             CATCH_ALL
  3433.                 _fDstLink.link = (ODLink*) kODNULL;
  3434.                 _fDstLink.start = -1;
  3435.                 _fDstLink.end = -1;
  3436.                 ODDisposePtr(newLinkInfo.kind);
  3437.             ENDTRY
  3438.         }
  3439.     }
  3440.     
  3441.     _fDirty = kODTrue;
  3442.     _fDraft->SetChangedFromPrev(ev);
  3443.     
  3444.     SOM_CATCH_ALL
  3445.         PRINT("IncorporateTestDragContent caught exception\n");
  3446.     SOM_ENDTRY
  3447. }
  3448.  
  3449. SOM_Scope void  SOMLINK AppleTestDrag_DragTextIncorporateStyledTextContent(AppleTestDrag_DragText *somSelf, Environment *ev,
  3450.         ODStorageUnit* su,
  3451.         ODUpdateID change)
  3452. {
  3453.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3454.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","IncorporateStyledTextContent");
  3455.  
  3456.     SOM_TRY
  3457.  
  3458.     ODSShort        delStart = (**(_fDocument->theTE)).selStart;
  3459.     ODSShort        delEnd = (**(_fDocument->theTE)).selEnd;
  3460.     ODSShort        newEnd;
  3461.     ODValue            value = kODNULL;
  3462.     ODULong            size = 0;
  3463.     ODULong            stsize = 0;
  3464.     Handle            textHandle = kODNULL;
  3465.     StScrpHandle    styleHandle = kODNULL;
  3466.     
  3467.     // Look first for a styled text property; only use 'styl' + 'TEXT' if not found
  3468.     if  (su->Exists(ev, kODPropContents, kODApplestxt, 0) )
  3469.     {
  3470.         su->Focus(ev, kODPropContents, kODPosUndefined, 
  3471.                        kODApplestxt, (ODValueIndex)0, kODPosUndefined);
  3472.         
  3473.         ReadStyledText(ev, su, &textHandle, &styleHandle);
  3474.         size = GetHandleSize(textHandle);
  3475.         stsize = GetHandleSize((Handle) styleHandle);
  3476.     }
  3477.     else
  3478.     {
  3479.         su->Focus(ev, kODPropContents, kODPosUndefined, 
  3480.                        kODAppleTEXT, (ODValueIndex)0, kODPosUndefined);
  3481.         
  3482.         size = su->GetSize(ev);
  3483.  
  3484.         textHandle = (Handle) ODNewHandle(size);
  3485.         value = ODLockHandle((ODHandle) textHandle);
  3486.         su->SetOffset(ev, 0);
  3487.         StorageUnitGetValue(su, ev, size, value);
  3488.         ODUnlockHandle((ODHandle) textHandle);
  3489.  
  3490.         su->Focus(ev, kODPropContents, kODPosUndefined, 
  3491.                        kODApplestyl, (ODValueIndex)0, kODPosUndefined);
  3492.         
  3493.         stsize = su->GetSize(ev);
  3494.         styleHandle = (StScrpHandle) ODNewHandle(stsize);
  3495.         
  3496.         value = ODLockHandle((ODHandle) styleHandle);
  3497.         su->SetOffset(ev, 0);
  3498.         StorageUnitGetValue(su, ev, stsize, value);
  3499.         ODUnlockHandle((ODHandle) styleHandle);
  3500.     
  3501.         // PRINT("Size of 'styl' handle is %d\n", stsize);
  3502.         // PRINT("First short of 'styl' resource is %d\n", *((short*)(*style)));
  3503.         // PRINT("Computed length of 'styl' resource is %d\n", sizeof(short)+(*((short*)(*style)))*sizeof(ScrpSTElement));
  3504.     }
  3505.  
  3506.     TEDelete(_fDocument->theTE);
  3507.  
  3508.     value = ODLockHandle((ODHandle) textHandle);
  3509.     if ( stsize > 0 )
  3510.         TEStyleInsert(value, size, styleHandle, _fDocument->theTE);
  3511.     else
  3512.         TEInsert(value, size, _fDocument->theTE);
  3513.     ODUnlockHandle((ODHandle) textHandle);
  3514.     
  3515.     ODDisposeHandle((Handle) textHandle);
  3516.     ODDisposeHandle((Handle) styleHandle);
  3517.         
  3518.     newEnd = (**(_fDocument->theTE)).selEnd;
  3519.     somSelf->AdjustLinks(ev, delStart, delEnd, newEnd, change);
  3520.     _fDirty = kODTrue;
  3521.     _fDraft->SetChangedFromPrev(ev);
  3522.  
  3523.     SOM_CATCH_ALL
  3524.     SOM_ENDTRY
  3525. }
  3526.  
  3527. SOM_Scope void  SOMLINK AppleTestDrag_DragTextIncorporateTextContent(AppleTestDrag_DragText *somSelf, Environment *ev,
  3528.         ODStorageUnit* su,
  3529.         ODUpdateID change)
  3530. {
  3531.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3532.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","IncorporateTextContent");
  3533.  
  3534.     ODSShort    delStart = (**(_fDocument->theTE)).selStart;
  3535.     ODSShort    delEnd = (**(_fDocument->theTE)).selEnd;
  3536.     ODSShort    newEnd;
  3537.     ODULong        size;
  3538.  
  3539.     SOM_TRY
  3540.  
  3541. //#ifdef SHOW_LINK_BORDERS
  3542. //    somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  3543. //#endif
  3544.  
  3545.     // Determine how much text will be pasted
  3546.     su->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, (ODValueIndex)0, kODPosUndefined);
  3547.     size = su->GetSize(ev);
  3548.  
  3549.     // Insist that this part contains fewer than 20K of text
  3550.     ODULong newSize = (**(_fDocument->theTE)).teLength - (delEnd-delStart) + size;
  3551.     
  3552.     if ( newSize > kMaxTextSize )
  3553.         THROW(kDragTextErrTooMuchText);
  3554.  
  3555.     // Delete the current selection
  3556.     TEDelete(_fDocument->theTE);
  3557.  
  3558.     if ( size > 0 )
  3559.     {
  3560.         ODValue    text = kODNULL;
  3561.         text = (ODValue) ODNewPtr(size, kDefaultHeapID);
  3562.     
  3563.         su->SetOffset(ev, 0);
  3564.         StorageUnitGetValue(su, ev, size, text);
  3565.     
  3566.         TEInsert((void*)text, size, _fDocument->theTE);
  3567.         
  3568.         ODDisposePtr((ODPtr) text);
  3569.     }
  3570.         
  3571.     newEnd = (**(_fDocument->theTE)).selEnd;
  3572.     somSelf->AdjustLinks(ev, delStart, delEnd, newEnd, change);
  3573.     _fDirty = kODTrue;
  3574.     _fDraft->SetChangedFromPrev(ev);
  3575.  
  3576.     SOM_CATCH_ALL
  3577.     SOM_ENDTRY
  3578. }
  3579.  
  3580. SOM_Scope ODStorageUnit* AppleTestDrag_DragTextTranslateContentKind(
  3581.         AppleTestDrag_DragText *somSelf, Environment *ev,
  3582.         ODStorageUnit* contentSU,
  3583.         ODType desiredKind,
  3584.         ODType translateKind)
  3585. {
  3586.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3587.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","TranslateContentKind");
  3588.  
  3589.     ODStorageUnit* newSU = kODNULL;
  3590.     ODStorageUnitView* fromView = kODNULL;
  3591.     ODStorageUnitView* toView = kODNULL;
  3592.     ODByteArray dataBA;
  3593.  
  3594.     SOM_TRY
  3595.     
  3596.     ODTranslation* translation = contentSU->GetSession(ev)->GetTranslation(ev); 
  3597.  
  3598.     // For now, translate into a new storage unit in this draft
  3599.     newSU = _fDraft->CreateStorageUnit(ev);
  3600.  
  3601.     ODVolatile(fromView);
  3602.     ODVolatile(toView);
  3603.     ODVolatile(newSU);
  3604.                 
  3605.     TRY
  3606.         contentSU->Focus(ev, kODPropContents, kODPosUndefined, (ODValueType) translateKind, 0, kODPosUndefined);
  3607.         fromView = contentSU->CreateView(ev);  // set from view to the original value
  3608.  
  3609.         SUForceFocus(ev, newSU, kODPropContents, desiredKind);
  3610.         toView = newSU->CreateView(ev); // set to view to the this new value
  3611.  
  3612.         ODTranslateResult transResult = translation->TranslateView(ev, fromView, toView);  // do the actual translation
  3613.         if ( transResult != kODCanTranslate )
  3614.             THROW(kDragTextErrCannotTranslate);
  3615.  
  3616.         if ( IsKind(desiredKind, kODApplestyl) )
  3617.         {
  3618.             if ( SUExistsThenFocus(ev, contentSU, kODPropContents, kODAppleTEXT) )
  3619.             {
  3620.                 ODULong size = contentSU->GetSize(ev);
  3621.                 contentSU->GetValue(ev, size, &dataBA);
  3622.             
  3623.                 SUForceFocus(ev, newSU, kODPropContents, kODAppleTEXT);
  3624.                 newSU->SetValue(ev, &dataBA);
  3625.                 ODDisposePtr(dataBA._buffer);
  3626.             }
  3627.         }
  3628.     CATCH_ALL
  3629.         ODDeleteObject(fromView);
  3630.         ODDeleteObject(toView);
  3631.         ODReleaseObject(ev, newSU);    // $$$$$ Should remove from draft
  3632.         RERAISE;
  3633.     ENDTRY
  3634.  
  3635.     ODDeleteObject(fromView);
  3636.     ODDeleteObject(toView);
  3637.  
  3638.     return newSU;
  3639.  
  3640.     SOM_CATCH_ALL
  3641.     SOM_ENDTRY
  3642.     return kODNULL;
  3643. }
  3644.  
  3645. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoPaste(AppleTestDrag_DragText *somSelf, Environment *ev,
  3646.         ODFrame* frame,
  3647.         ODType desiredKind,
  3648.         ODType translateKind)
  3649. {
  3650.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3651.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoPaste");
  3652.  
  3653.     ODUnused(frame);
  3654.     
  3655.     SOM_TRY
  3656.  
  3657.     if ( frame->GetLinkStatus(ev) == kODInLinkDestination )
  3658.     {
  3659.         if ( !frame->EditInLink(ev) )
  3660.             // Put up an alert;
  3661.             WARN("DragText: The destination of a link cannot be edited.");
  3662.         return kODTrue;
  3663.     }
  3664.  
  3665.     ODFrame* clipboardFocus = _fSession->GetArbitrator(ev)->AcquireFocusOwner(ev, _fClipboardFocus);
  3666.     WASSERTM((frame == clipboardFocus), "DragText::DoPaste - Clipboard focus not owned");
  3667.     ODReleaseObject(ev, clipboardFocus);
  3668.  
  3669.     ODClipboard* clipboard = _fSession->GetClipboard(ev);
  3670.  
  3671.     TRY
  3672.         ODStorageUnit* clipBSU = clipboard->GetContentStorageUnit(ev);
  3673.         ODStorageUnit* contentSU;
  3674.         ODUpdateID        updateID = _fSession->UniqueUpdateID(ev);
  3675.  
  3676.         if ( translateKind == kODNULL )
  3677.             contentSU = clipBSU;
  3678.         else
  3679.             // Need to translate prior to incorporating the content
  3680.             contentSU = somSelf->TranslateContentKind(ev, clipBSU, desiredKind, translateKind);
  3681.  
  3682. #ifdef SUPPORT_TESTDRAG_KIND
  3683.         if ( PasteThisKind (ev, contentSU, kODKindTestDrag, desiredKind) )
  3684.         {
  3685.             somSelf->OpenWhitespace(ev, contentSU, kODKindTestDrag);
  3686.             somSelf->IncorporateTestDragContent(ev, contentSU, updateID, kODClonePaste);
  3687.             frame->ContentUpdated(ev, updateID);
  3688.         }
  3689.         else
  3690. #endif
  3691.         if ( PasteThisKind (ev, contentSU, kODApplestxt, desiredKind) 
  3692.             ||
  3693.              PasteThisKind (ev, contentSU, kODApplestyl, desiredKind) )
  3694.         {
  3695.             somSelf->OpenWhitespace(ev, contentSU, kODAppleTEXT);
  3696.             somSelf->IncorporateStyledTextContent(ev, contentSU, updateID);
  3697.             frame->ContentUpdated(ev, updateID);
  3698.         }
  3699.         else if ( PasteThisKind (ev, contentSU, kODAppleTEXT, desiredKind) )
  3700.         {
  3701.             somSelf->OpenWhitespace(ev, contentSU, kODAppleTEXT);
  3702.             somSelf->IncorporateTextContent(ev, contentSU, updateID);
  3703.             frame->ContentUpdated(ev, updateID);
  3704.         }
  3705.         
  3706.         if ( translateKind != kODNULL )
  3707.         {
  3708.             // Focus to the contents property and remove it
  3709.             contentSU->Focus(ev, kODPropContents, kODPosUndefined, (ODValueType) kODNULL, 0, kODPosUndefined);
  3710.             contentSU->Remove(ev);
  3711.             ODReleaseObject(ev, contentSU);    // $$$$$ Should remove storage unit
  3712.         }
  3713.     CATCH_ALL
  3714.         if ( ErrorCode() == kDragTextErrTooMuchText )
  3715.             ShowInfoAlert(ev, "\pToo much text to paste here.", frame);
  3716.         else
  3717.             ShowErrorAlert(ev, "\pError during Paste.", ErrorCode(), frame);
  3718.     ENDTRY
  3719.  
  3720.     return kODTrue;
  3721.  
  3722.     SOM_CATCH_ALL
  3723.     SOM_ENDTRY
  3724.     return kODFalse;
  3725. }
  3726.  
  3727. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoPaste(AppleTestDrag_DragText *somSelf, Environment *ev,
  3728.         UndoInfo* action,
  3729.         ODUShort selector)
  3730. {
  3731.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3732.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoPaste");
  3733.  
  3734.      ODUnused(action);
  3735.      ODUnused(selector);
  3736. }
  3737.  
  3738. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoPasteAs(AppleTestDrag_DragText *somSelf, Environment *ev,
  3739.         ODFrame* frame)
  3740. {
  3741.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3742.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoPasteAs");
  3743.  
  3744.     SOM_TRY
  3745.  
  3746.     ODPasteAsResult    pasteAsRslt;
  3747.         pasteAsRslt.selectedKind = kODNULL;
  3748.         pasteAsRslt.translateKind = kODNULL;
  3749.         pasteAsRslt.editor = kODNULL;
  3750.     
  3751.     TempODPasteAsResult tempResult = &pasteAsRslt; 
  3752.     // DMc: ensure deletion of selectedKind, translateKind, editor
  3753.  
  3754.     ODBoolean        result = kODTrue;
  3755.  
  3756.     if ( frame->GetLinkStatus(ev) == kODInLinkDestination )
  3757.     {
  3758.         if ( !frame->EditInLink(ev) )
  3759.             // Put up an alert;
  3760.             WARN("DragText: The destination of a link cannot be edited.");
  3761.         return kODTrue;
  3762.     }
  3763.  
  3764.     ODFrame* clipboardFocus = _fSession->GetArbitrator(ev)->AcquireFocusOwner(ev, _fClipboardFocus);
  3765.     WASSERTM((frame == clipboardFocus), "DragText::DoPasteAs - Clipboard focus not owned");
  3766.     ODReleaseObject(ev, clipboardFocus);
  3767.  
  3768.     ODClipboard* clipboard = _fSession->GetClipboard(ev);
  3769.     ODFrameFacetIterator* facets = (ODFrameFacetIterator*) kODNULL;
  3770.  
  3771. #ifdef SHOW_CLIPBOARD_PASTE_AS_DIALOG
  3772.     TRY
  3773.         ODStorageUnit* clipContentSU = clipboard->GetContentStorageUnit(ev);
  3774.         facets = frame->CreateFacetIterator(ev);
  3775.         if ( clipboard->ShowPasteAsDialog(ev,
  3776.                     somSelf->CanPasteLink(ev, 
  3777.                         clipContentSU, 
  3778.                         (clipboard->GetUpdateID(ev) == _fClipboardUpdateID),
  3779.                         (**(_fDocument->theTE)).selStart,
  3780.                         (**(_fDocument->theTE)).selEnd),
  3781.                     kODPasteAsMergeOnly,
  3782.                     facets->First(ev),
  3783.                     _fSession->Tokenize(ev, kODViewAsFrame),
  3784.                     &pasteAsRslt) )
  3785.         {
  3786.             if ( pasteAsRslt.pasteLinkSetting != kODFalse )
  3787.             {
  3788.                 // $$$$$ Cannot yet paste a link if translation is required
  3789.                 if ( pasteAsRslt.translateKind != kODNULL )
  3790.                     THROW(kDragTextErrTranslateLinkError);
  3791.                 result = somSelf->DoPasteLink(ev, frame, pasteAsRslt.selectedKind);
  3792.             }
  3793.             else
  3794.                 result = somSelf->DoPaste(ev, frame, pasteAsRslt.selectedKind, pasteAsRslt.translateKind);
  3795.         }
  3796.     CATCH_ALL
  3797.         PRINT("DoPasteAs caught error %d\n", ErrorCode());
  3798.     ENDTRY
  3799.  
  3800.     ODDeleteObject(facets);
  3801. #else
  3802.     result = somSelf->DoPasteLink(ev, frame, kODAppleTEXT);
  3803. #endif
  3804.  
  3805.     return result;
  3806.  
  3807.     SOM_CATCH_ALL
  3808.     SOM_ENDTRY
  3809.     return kODTrue;
  3810. }
  3811.  
  3812. SOM_Scope void  SOMLINK AppleTestDrag_DragTextPasteLinkFromSU(AppleTestDrag_DragText *somSelf, Environment *ev,
  3813.         ODFrame*        frame,
  3814.         ODType            desiredKind,
  3815.         ODStorageUnit*    su)
  3816. {
  3817.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3818.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","PasteLinkFromSU");
  3819.  
  3820.     SOM_TRY
  3821.  
  3822.     if (su->Exists(ev, kODPropLinkSpec, (ODValueType) kODNULL, 0) != kODFalse) {
  3823.         ODLinkSpec* ls = _fDraft->CreateLinkSpec(ev, kODNULL, kODNULL);
  3824.         su->Focus(ev, kODPropLinkSpec, kODPosUndefined, (ODValueType) kODNULL, 0, kODPosUndefined);
  3825.         ls->ReadLinkSpec(ev, su);
  3826.         TRY
  3827.             _fDstLink.link = _fDraft->AcquireLink(ev, (ODStorageUnitID) kODNULL, ls);
  3828.             if (_fDstLink.link != (ODLink*) kODNULL) {
  3829.                 ODSShort delStart = (**(_fDocument->theTE)).selStart;
  3830.                 ODSShort delEnd = (**(_fDocument->theTE)).selEnd;
  3831.  
  3832.                 // Delete the existing text and wait for LinkUpdated() to be
  3833.                 // called to get the text
  3834.                 TEDelete(_fDocument->theTE);
  3835.                 
  3836.                 _fDstLink.info.change = _fDstLink.link->GetUpdateID(ev);
  3837.                 GetDateTime((ODTime*) &_fDstLink.info.creationTime);
  3838.                 _fDstLink.info.changeTime = _fDstLink.info.creationTime;
  3839.                 _fDstLink.info.kind = (ODType) ODISOStrFromCStr(desiredKind);
  3840.                 _fDstLink.info.autoUpdate = kODTrue;
  3841.  
  3842.                 // Adjust other links due to this change
  3843.                 // Special cased for source links only for now
  3844.                 // Assume no overlapping links or recursion problems
  3845.                 somSelf->AdjustSrcLinks(ev, delStart, delEnd, delStart, _fDstLink.info.change);
  3846.                 _fDstLink.start = delStart;
  3847.                 _fDstLink.end = delStart;
  3848.                 // Now register as a dependent of the link
  3849.                 // Specify kODUnknownUpdate to force getting the text.
  3850.                 // By default the destination is automatically updated.
  3851.                 _fDstLink.link->RegisterDependent(ev, _fPartWrapper, kODUnknownUpdate);
  3852.                 // Put the insertion point after the text
  3853.                 TESetSelect(_fDstLink.end, _fDstLink.end, _fDocument->theTE); 
  3854.  
  3855.                 frame->ContentUpdated(ev, _fDstLink.info.change);
  3856.                 _fDirty = kODTrue;
  3857.                 _fDraft->SetChangedFromPrev(ev);
  3858.                 }
  3859.         CATCH_ALL
  3860.             if( ErrorCode()==kODErrDocNotSaved )
  3861.                 WARN("Source document not saved!");
  3862.         ENDTRY
  3863.         ODDeleteObject(ls);
  3864.         }
  3865.  
  3866.     SOM_CATCH_ALL
  3867.     SOM_ENDTRY
  3868. }
  3869.  
  3870. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoPasteLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  3871.         ODFrame* frame,
  3872.         ODType desiredKind)
  3873. {
  3874.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3875.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoPasteLink");
  3876.  
  3877.     ODUnused(frame);
  3878.     
  3879.     SOM_TRY
  3880.  
  3881.     ODFrame* clipboardFocus = _fSession->GetArbitrator(ev)->AcquireFocusOwner(ev, _fClipboardFocus);
  3882.     WASSERTM((frame == clipboardFocus), "DragText::DoPasteLink - Clipboard focus not owned");
  3883.     ODReleaseObject(ev, clipboardFocus);
  3884.     
  3885.     ODClipboard* clipboard = _fSession->GetClipboard(ev);
  3886.  
  3887.     TRY
  3888.         ODStorageUnit* clipBSU = clipboard->GetContentStorageUnit(ev);
  3889.         somSelf->PasteLinkFromSU(ev, frame, desiredKind, clipBSU);
  3890.     CATCH_ALL
  3891.     ENDTRY
  3892.  
  3893.     return kODTrue;
  3894.  
  3895.     SOM_CATCH_ALL
  3896.     SOM_ENDTRY
  3897.     return kODTrue;
  3898. }
  3899.  
  3900. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoPasteLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  3901.         UndoInfo* action,
  3902.         ODUShort selector)
  3903. {
  3904.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3905.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoPasteLink");
  3906.  
  3907.     ODUnused(action);
  3908.     ODUnused(selector);
  3909. }
  3910.  
  3911. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextSelectionInLinkSource(AppleTestDrag_DragText *somSelf, Environment *ev)
  3912. {
  3913.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3914.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","SelectionInLinkSource");
  3915.  
  3916.     ODBoolean inSourceLink = kODFalse;
  3917.     ODSShort selStart = (**(_fDocument->theTE)).selStart;
  3918.     ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  3919.     
  3920.     if (_fSrcLink.linkSource == ((ODLinkSource*) kODNULL))
  3921.         inSourceLink = kODFalse;
  3922.     else if ((selStart >= _fSrcLink.start) && (selStart < _fSrcLink.end))
  3923.         inSourceLink = kODTrue;
  3924.     else if ((selEnd > _fSrcLink.start) && (selEnd <= _fSrcLink.end))
  3925.         inSourceLink = kODTrue;
  3926.     else if ((selStart <= _fSrcLink.start) && (selEnd >= _fSrcLink.end))
  3927.         inSourceLink = kODTrue;
  3928.     else
  3929.         inSourceLink = kODFalse;
  3930.  
  3931.     return inSourceLink;
  3932. }
  3933.  
  3934. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextSelectionInLinkDestination(AppleTestDrag_DragText *somSelf, Environment *ev)
  3935. {
  3936.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3937.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","SelectionInLinkDestination");
  3938.  
  3939.     ODBoolean inDestinationLink = kODFalse;
  3940.     
  3941.     ODSShort selStart = (**(_fDocument->theTE)).selStart;
  3942.     ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  3943.     
  3944.     if ((selStart >= _fDstLink.start) && (selStart < _fDstLink.end))
  3945.         inDestinationLink = kODTrue;
  3946.     else if ((selEnd > _fDstLink.start) && (selEnd <= _fDstLink.end))
  3947.         inDestinationLink = kODTrue;
  3948.     else if ((selStart <= _fDstLink.start) && (selEnd >= _fDstLink.end))
  3949.         inDestinationLink = kODTrue;
  3950.  
  3951.     return inDestinationLink;
  3952. }
  3953.  
  3954. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoLinkInfo(AppleTestDrag_DragText *somSelf, Environment *ev,
  3955.         ODFrame* frame)
  3956. {
  3957.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  3958.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoLinkInfo");
  3959.  
  3960.     // For now, suppress any errors returned by ShowInfo.  The ShowInfo methods may
  3961.     // return a broken link error; rather than handling them by alerting the user and
  3962.     // removing the link object, just do nothing.
  3963.     TRY
  3964.  
  3965.     ODBoolean                result = kODTrue;
  3966.     ODLinkInfoResult        infoResult;
  3967.     ODFrameFacetIterator*    facets = frame->CreateFacetIterator(ev);
  3968.  
  3969.     if (somSelf->SelectionInLinkSource(ev))
  3970.     {
  3971.         if ( _fSrcLink.linkSource->ShowLinkSourceInfo(ev, 
  3972.                                         facets->First(ev),
  3973.                                         _fSrcLink.change,
  3974.                                         kODTrue,
  3975.                                         &infoResult) )
  3976.         {
  3977.             switch (infoResult.action) {
  3978.             case kODLinkInfoBreakLink:
  3979. #ifdef SHOW_LINK_BORDERS
  3980.                 somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  3981. #endif
  3982.                 _fSrcLink.linkSource->SetSourcePart(ev, (ODStorageUnit*) kODNULL);
  3983.                 _fSrcLink.linkSource->Release(ev);
  3984.                 _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  3985.                 _fSrcLink.start = -1;
  3986.                 _fSrcLink.end = -1;
  3987.                 _fSrcLink.change = kODUnknownUpdate;
  3988.                 _fDirty = kODTrue;
  3989.                 _fDraft->SetChangedFromPrev(ev);
  3990.                 break;
  3991.             case kODLinkInfoUpdateNow:
  3992.                 somSelf->LinkChanged(ev, &_fSrcLink, _fSrcLink.change);
  3993.                 break;
  3994.             case kODLinkInfoOk:
  3995.                 if ( infoResult.autoUpdate != _fSrcLink.linkSource->IsAutoUpdate(ev) )
  3996.                 {
  3997.                     _fSrcLink.linkSource->SetAutoUpdate(ev, infoResult.autoUpdate);
  3998.                     if ( infoResult.autoUpdate )
  3999.                         if ( _fSrcLink.change != _fSrcLink.linkSource->GetUpdateID(ev) )
  4000.                             {
  4001.                             // Changed from manual to automatic updates, and the link
  4002.                             // is out of date; update it now
  4003.                             somSelf->LinkChanged(ev, &_fSrcLink, _fSrcLink.change);
  4004.                             }
  4005.                 }
  4006.                 break;
  4007.             default:
  4008.                 break;
  4009.             }
  4010.         }
  4011.     }
  4012.     else if (somSelf->SelectionInLinkDestination(ev))
  4013.     {
  4014.         if ( _fDstLink.link->ShowLinkDestinationInfo(ev, 
  4015.                                         facets->First(ev),
  4016.                                         &_fDstLink.info,
  4017.                                         kODTrue,
  4018.                                         &infoResult) )
  4019.         {
  4020.             switch (infoResult.action) {
  4021.             case kODLinkInfoFindSource:
  4022.                 TRY
  4023.                     PRINT("DragText %d: Calling ShowSourceContent\n", somSelf->GetID(ev));
  4024.                     _fDstLink.link->ShowSourceContent(ev);
  4025.                     PRINT("DragText %d: Returned from ShowSourceContent\n", somSelf->GetID(ev));
  4026.                 CATCH_ALL
  4027.                     PRINT("DragText %d: Error %d returned by ODLink::ShowSourceContent\n", somSelf->GetID(ev), ErrorCode());
  4028.                     ShowErrorAlert(ev, "\pCannot show link source", ErrorCode(), frame);
  4029.                 ENDTRY;
  4030.                 break;
  4031.             case kODLinkInfoBreakLink:
  4032.                 somSelf->RemoveDstLink(ev);
  4033.                 break;
  4034.             case kODLinkInfoUpdateNow:
  4035.                 somSelf->LinkUpdated(ev, _fDstLink.link, _fDstLink.link->GetUpdateID(ev));
  4036.                 break;
  4037.             case kODLinkInfoOk:
  4038.                 if ( infoResult.autoUpdate != _fDstLink.info.autoUpdate )
  4039.                 {
  4040.                     _fDstLink.info.autoUpdate = infoResult.autoUpdate;
  4041.                     // Real parts must be careful of multiple destinations of the same link!
  4042.                     if ( infoResult.autoUpdate )
  4043.                         _fDstLink.link->RegisterDependent(ev, _fPartWrapper, _fDstLink.info.change);
  4044.                     else
  4045.                         _fDstLink.link->UnregisterDependent(ev, _fPartWrapper);
  4046.                 }
  4047.                 break;
  4048.             default:
  4049.                 break;
  4050.             }
  4051.         }
  4052. #ifdef SHOW_LINK_BORDERS
  4053.         somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  4054. #endif
  4055.     }
  4056.  
  4057.     ODDeleteObject(facets);
  4058.  
  4059.     return kODTrue;    // Always handle the event
  4060.  
  4061.     CATCH_ALL
  4062.     ENDTRY
  4063.     return kODFalse;
  4064. }
  4065.  
  4066. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRevealLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  4067.         ODLinkSource* linkSource)
  4068. {
  4069.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4070.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RevealLink");
  4071.  
  4072.     SOM_TRY
  4073.  
  4074.     if ( _fSrcLink.linkSource != linkSource )
  4075.         THROW(kDragTextErrInvalidLink);
  4076.  
  4077.     ODFrame* frame = (ODFrame*) _fDisplayFrames->First();
  4078.     
  4079.     if ( frame != (ODFrame*) kODNULL )
  4080.     {
  4081.         ProcessSerialNumber psn;
  4082.         GetCurrentProcess(&psn);
  4083.         SetFrontProcess(&psn);
  4084.  
  4085. #ifdef SHOW_LINK_BORDERS
  4086.         somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  4087. #endif
  4088.  
  4089.         ODFrame* containingFrame = frame->AcquireContainingFrame(ev);
  4090.  
  4091.         if ( containingFrame != (ODFrame*) kODNULL )
  4092.         {
  4093.             ODPart* part = containingFrame->AcquirePart(ev);
  4094.  
  4095.             // Sample parts currently THROW so catch and ignore
  4096.             TRY
  4097.                 if ( part )
  4098.                     part->RevealFrame(ev, frame, (ODShape*) kODNULL);
  4099.             CATCH_ALL
  4100.             ENDTRY
  4101.             
  4102.             ODReleaseObject(ev, part);
  4103.             containingFrame->Release(ev);
  4104.         }
  4105.  
  4106.         somSelf->ActivateFrame(ev, frame);
  4107.         if (_fHasFoci)
  4108.         {
  4109.             TESetSelect(_fSrcLink.start, _fSrcLink.end, _fDocument->theTE); 
  4110.         }
  4111.     }
  4112.     else
  4113.     {
  4114.         // Otherwise open a new window
  4115.     }
  4116.     
  4117.     SOM_CATCH_ALL
  4118.         PRINT("DragText %d: RevealLink :Error %d caught\n", somSelf->GetID(ev), ErrorCode());
  4119.     SOM_ENDTRY
  4120. }
  4121.  
  4122. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextShowAboutBox(AppleTestDrag_DragText *somSelf, Environment *ev,
  4123.         ODFrame* frame)
  4124. {
  4125.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4126.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ShowAboutBox");
  4127.  
  4128.     SOM_TRY
  4129.  
  4130.     ODTypeToken modalFocus = _fSession->Tokenize(ev, kODModalFocus);
  4131.  
  4132.     ODFrame* currentOwner = _fSession->GetArbitrator(ev)->AcquireFocusOwner(ev, modalFocus);
  4133.     if (_fSession->GetArbitrator(ev)->RequestFocus(ev, modalFocus, frame))
  4134.     {
  4135.         short itemHit;
  4136.         ParamText("\pDragText Part…",0,0,0);
  4137.         SetCursor(&ODQDGlobals.arrow);
  4138.         ModalFilterUPP modalFilter = NewModalFilterProc(DragTextDialogFilter);
  4139.         
  4140.         ODSLong savedRefNum;
  4141.         BeginUsingLibraryResources(savedRefNum);
  4142.         DialogPtr aboutDialog = GetNewDialog(kDragText_AboutBoxID ,0, (WindowPtr) -1L);
  4143.         EndUsingLibraryResources(savedRefNum);
  4144.  
  4145.         if (aboutDialog)
  4146.         {
  4147.             _fSession->GetWindowState(ev)->DeactivateFrontWindows(ev);
  4148.             ((WindowPeek)aboutDialog)->refCon = (long) _fSession;
  4149.             ShowWindow(aboutDialog);
  4150.             ModalDialog(modalFilter, &itemHit);
  4151.             DisposeDialog(aboutDialog);
  4152.             _fSession->GetWindowState(ev)->ActivateFrontWindows(ev);
  4153.         }
  4154.         DisposeRoutineDescriptor(modalFilter);
  4155.  
  4156.         _fSession->GetArbitrator(ev)->TransferFocus(ev, modalFocus, frame, currentOwner);        
  4157.     }
  4158.     else
  4159.         SysBeep(2);
  4160.     ODReleaseObject(ev, currentOwner);
  4161.     
  4162.     return kODTrue;
  4163.  
  4164.     SOM_CATCH_ALL
  4165.     SOM_ENDTRY
  4166.     return kODTrue;
  4167. }
  4168.  
  4169. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoClear(AppleTestDrag_DragText *somSelf, Environment *ev,
  4170.         ODFrame* frame)
  4171. {
  4172.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4173.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoClear");
  4174.  
  4175.     SOM_TRY
  4176.  
  4177.     ODSShort delStart = (**(_fDocument->theTE)).selStart;
  4178.     ODSShort delEnd = (**(_fDocument->theTE)).selEnd;
  4179.  
  4180.     if (delEnd > delStart)
  4181.     {
  4182.         if ( frame->GetLinkStatus(ev) == kODInLinkDestination )
  4183.         {
  4184.             if ( !frame->EditInLink(ev) )
  4185.                 // Put up an alert;
  4186.                 WARN("DragText: The destination of a link cannot be edited.");
  4187.             return kODTrue;
  4188.         }
  4189.  
  4190.         TEDelete(_fDocument->theTE);
  4191.  
  4192.         ODUpdateID updateID = _fSession->UniqueUpdateID(ev);
  4193.         somSelf->AdjustLinks(ev, delStart, delEnd, delStart, updateID);
  4194.         frame->ContentUpdated(ev, updateID);
  4195.     
  4196.         _fDirty = kODTrue;
  4197.         _fDraft->SetChangedFromPrev(ev);
  4198.     }
  4199.  
  4200.     return kODTrue;
  4201.  
  4202.     SOM_CATCH_ALL
  4203.     SOM_ENDTRY
  4204.     return kODTrue;
  4205. }
  4206.  
  4207. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoClear(AppleTestDrag_DragText *somSelf, Environment *ev,
  4208.         UndoInfo* action,
  4209.         ODUShort selector)
  4210. {
  4211.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4212.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoClear");
  4213.  
  4214.     ODUnused(action);
  4215.     ODUnused(selector);
  4216. }
  4217.  
  4218. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoSelectAll(AppleTestDrag_DragText *somSelf, Environment *ev,
  4219.         ODFrame* frame)
  4220. {
  4221.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4222.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoSelectAll");
  4223.  
  4224.     ODUnused(frame);
  4225.     
  4226.     TESetSelect(0,32767,_fDocument->theTE);
  4227.     
  4228.     return kODTrue;
  4229. }
  4230.  
  4231. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoSampleOne(AppleTestDrag_DragText *somSelf, Environment *ev,
  4232.         ODFrame* frame)
  4233. {
  4234.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4235.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoSampleOne");
  4236.  
  4237.     SOM_TRY
  4238.  
  4239.     char* defOne = "As I sailed into Shadow, a white bird of my desire came and sat upon my right shoulder, and I wrote a note and tied it to its leg and sent in on its way.";
  4240.     TESetText(defOne, strlen(defOne), _fDocument->theTE);
  4241.     somSelf->RemoveSrcLink(ev);
  4242.     somSelf->RemoveDstLink(ev);
  4243.     ODFrameFacetIterator* facet = frame->CreateFacetIterator(ev);
  4244.     facet->First(ev)->Invalidate(ev, kODNULL, kODNULL);
  4245.     ODDeleteObject(facet);
  4246.     _fFrameShapeChanged = true;
  4247.     _fDirty = kODTrue;
  4248.     _fDraft->SetChangedFromPrev(ev);
  4249.     return kODTrue;
  4250.  
  4251.     SOM_CATCH_ALL
  4252.     SOM_ENDTRY
  4253.     return kODTrue;
  4254. }
  4255.  
  4256. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoSampleOne(AppleTestDrag_DragText *somSelf, Environment *ev,
  4257.         UndoInfo* action,
  4258.         ODUShort selector)
  4259. {
  4260.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4261.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoSampleOne");
  4262.  
  4263.     ODUnused(action);
  4264.     ODUnused(selector);
  4265. }
  4266.  
  4267. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoSampleTwo(AppleTestDrag_DragText *somSelf, Environment *ev,
  4268.         ODFrame* frame)
  4269. {
  4270.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4271.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoSampleTwo");
  4272.  
  4273.     SOM_TRY
  4274.  
  4275.     char* defTwo = "A black bird of my desire came and sat on my left shoulder, and I wrote a note and tied it to its leg and sent it off into the west.";
  4276.     TESetText(defTwo, strlen(defTwo), _fDocument->theTE);
  4277.     somSelf->RemoveSrcLink(ev);
  4278.     somSelf->RemoveDstLink(ev);
  4279.     ODFrameFacetIterator* facet = frame->CreateFacetIterator(ev);
  4280.     facet->First(ev)->Invalidate(ev, kODNULL, kODNULL);
  4281.     ODDeleteObject(facet);
  4282.     _fFrameShapeChanged = true;
  4283.     _fDirty = kODTrue;
  4284.     _fDraft->SetChangedFromPrev(ev);
  4285.     return kODTrue;
  4286.  
  4287.     SOM_CATCH_ALL
  4288.     SOM_ENDTRY
  4289.     return kODTrue;
  4290. }
  4291.  
  4292. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoSampleTwo(AppleTestDrag_DragText *somSelf, Environment *ev,
  4293.         UndoInfo* action,
  4294.         ODUShort selector)
  4295. {
  4296.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4297.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoSampleTwo");
  4298.  
  4299.     ODUnused(action);
  4300.     ODUnused(selector);
  4301. }
  4302.  
  4303. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoTyping(AppleTestDrag_DragText *somSelf, Environment *ev,
  4304.         UndoInfo* action,
  4305.         ODUShort selector)
  4306. {
  4307.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4308.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoTyping");
  4309.  
  4310.     ODUnused(action);
  4311.     ODUnused(selector);
  4312. }
  4313.  
  4314. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoRedoStyling(AppleTestDrag_DragText *somSelf, Environment *ev,
  4315.         UndoInfo* action,
  4316.         ODUShort selector)
  4317. {
  4318.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4319.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoRedoStyling");
  4320.  
  4321.     ODUnused(action);
  4322.     ODUnused(selector);
  4323. }
  4324.  
  4325. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextDoCommand(AppleTestDrag_DragText *somSelf, Environment *ev,
  4326.         ODFrame* frame,
  4327.         ODCommandID command)
  4328. {
  4329.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4330.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoCommand");
  4331.  
  4332.     SOM_TRY
  4333.  
  4334.     ODBoolean handled = kODFalse;
  4335.     short menu = 0;
  4336.     short item = 0;
  4337.  
  4338. #ifdef SHOW_LINK_BORDERS
  4339.     // Don't turn off link borders if the command is GetLinkInfo, so the user can
  4340.     // still see the link.
  4341.     if ( command != kODCommandGetPartInfo )
  4342.         somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  4343. #endif
  4344.  
  4345.     if (!_fMenuBar->IsCommandSynthetic(ev, command))
  4346.     {
  4347.         switch (command) {
  4348.             case kODCommandAbout:        handled = somSelf->ShowAboutBox(ev, frame); break;
  4349.             case kODCommandCut:         handled = somSelf->DoCut(ev, frame); break;
  4350.             case kODCommandCopy:        handled = somSelf->DoCopy(ev, frame); break;
  4351.             case kODCommandPaste:        handled = somSelf->DoPaste(ev, frame, kODNULL, kODNULL); break;
  4352.             case kODCommandPasteAs:        handled = somSelf->DoPasteAs(ev, frame); break;
  4353.             case kODCommandClear:        handled = somSelf->DoClear(ev, frame); break;
  4354.             case kODCommandSelectAll:    handled = somSelf->DoSelectAll(ev, frame); break;
  4355.             case kODCommandGetPartInfo:    handled = somSelf->DoLinkInfo(ev, frame); break;
  4356.             case cDTSampleOne:            handled = somSelf->DoSampleOne(ev, frame); break;
  4357.             case cDTSampleTwo:             handled = somSelf->DoSampleTwo(ev, frame); break;
  4358.             default:
  4359.                 break;
  4360.         }
  4361.     }
  4362.     else
  4363.     {
  4364.         _fMenuBar->GetMenuAndItem(ev, command, &menu, &item);    
  4365.         switch (menu)
  4366.         {
  4367.             case 141:
  4368.                 somSelf->HandleFontMenu(ev, item);
  4369.                 handled = kODTrue;
  4370.                 break;
  4371.             case 142:
  4372.                 somSelf->HandleSizeMenu(ev, item);
  4373.                 handled = kODTrue;
  4374.                 break;
  4375.             default:
  4376.                 break;
  4377.         }
  4378.     }
  4379.  
  4380.     HiliteMenu(0);    
  4381.     return (handled);
  4382.  
  4383.     SOM_CATCH_ALL
  4384.     SOM_ENDTRY
  4385.     return kODFalse;
  4386. }
  4387.  
  4388. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextHandleMenuEvent(AppleTestDrag_DragText *somSelf, Environment *ev,
  4389.         ODFrame* frame,
  4390.         ODEventData* event)
  4391. {
  4392.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4393.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleMenuEvent");
  4394.  
  4395.     SOM_TRY
  4396.  
  4397.     long menuResult = event->message;
  4398.     short menu = HiWord(menuResult);
  4399.     short item = LoWord(menuResult);
  4400.     ODCommandID command = _fMenuBar->GetCommand(ev, menu, item);
  4401.     ODBoolean handled = somSelf->DoCommand(ev, frame, command);
  4402.     return (handled);
  4403.  
  4404.     SOM_CATCH_ALL
  4405.     SOM_ENDTRY
  4406.     return kODFalse;
  4407. }
  4408.  
  4409. //-------------------------------------------------------------------------
  4410. // From Undo Protocol 
  4411. //-------------------------------------------------------------------------
  4412.  
  4413. SOM_Scope void  SOMLINK AppleTestDrag_DragTextPushUndoAction(AppleTestDrag_DragText *somSelf, Environment *ev,
  4414.         ODFrame* frame,
  4415.         ODCommandID command,
  4416.         ODULong start,
  4417.         ODULong end,
  4418.         ODPtr text,
  4419.         ODHandle style,
  4420.         StringPtr undoText,
  4421.         StringPtr redoText)
  4422. {
  4423.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4424.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","PushUndoAction");
  4425.  
  4426.     SOM_TRY
  4427.  
  4428. #if 0
  4429.     // create action object for Undo stack
  4430.     UndoInfo* uaction = (UndoInfo*) ODNewPtr(sizeof(UndoInfo), kDefaultHeapID);
  4431.     uaction->displayFrame = frame;
  4432.     uaction->command = command;
  4433.     uaction->start = start;
  4434.     uaction->end = end;
  4435.     uaction->text = text;
  4436.     uaction->style = style;
  4437.     
  4438.     // THERE ARE HELPER FUNCTIONS FOR THIS NOW IN PASCLSTR.H
  4439.     // create Undo and Redo menu item text
  4440.     //ODName* undoMenuText = (ODName*) ODNewPtr(sizeof(ODIText)+strlen(undoText), kDefaultHeapID);
  4441.     //undoMenuText->theScriptCode = smRoman;
  4442.     //undoMenuText->theLangCode = langEnglish;
  4443.     //undoMenuText->textLength = strlen(undoText);
  4444.     //memcpy(undoMenuText->theText, undoText, strlen(undoText));
  4445.  
  4446.     ODScriptCode script = FontToScript( GetAppFont() );
  4447.     ODLangCode lang = GetScriptVariable(script, smScriptLang);
  4448.     ODName* undoMenuText = CreateIText( script, lang, undoText);
  4449.     
  4450.     // THERE ARE HELPER FUNCTIONS FOR THIS NOW IN PASCLSTR.H
  4451.     // create Redo menu item text
  4452.     //ODName* redoMenuText = (ODName*) ODNewPtr(sizeof(ODIText)+strlen(redoText), kDefaultHeapID);
  4453.     //redoMenuText->theScriptCode = smRoman;
  4454.     //redoMenuText->theLangCode = langEnglish;
  4455.     //redoMenuText->textLength = strlen(redoText);
  4456.     //memcpy(redoMenuText->theText, redoText, strlen(redoText));
  4457.     
  4458.     // $$$$ Converted 7/14/94 but not tested
  4459.     // $$$$$ Doubt if this works. Commenting out-NP 1/24/95
  4460. //    ODName* redoMenuText = PStrToIntl(redoText, kODNULL);
  4461.     
  4462. //    _fSession->GetUndo(ev)->AddActionToHistory(ev, _fPartWrapper, (ODActionData)uaction,
  4463. //                                            kODSingleAction, undoMenuText,
  4464. //                                            redoMenuText);
  4465. //    _fUndoCount++;
  4466.     
  4467.     frame->Acquire(ev);
  4468. #endif /* 0 */
  4469.  
  4470.     SOM_CATCH_ALL
  4471.     SOM_ENDTRY
  4472. }
  4473.  
  4474. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUndoAction(AppleTestDrag_DragText *somSelf, Environment *ev,
  4475.         ODActionData* actionState)
  4476. {
  4477.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4478.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UndoAction");
  4479.  
  4480.     SOM_TRY
  4481.  
  4482. #if 0
  4483.     WASSERTM((_fUndoCount != 0), "Undo unexpected, internal count is zero");
  4484.     
  4485.     ODBoolean ok = kODFalse;
  4486.     UndoInfo* uaction = (UndoInfo*) actionState;
  4487.     
  4488.     OrderedCollectionIterator dispIter(_fDisplayFrames);
  4489.     for (ODFrame* frame = (ODFrame*)dispIter.First();
  4490.          dispIter.IsNotComplete();
  4491.          frame = (ODFrame*)dispIter.Next())
  4492.     {
  4493.         if (frame == (*uaction).displayFrame) ok = kODTrue;
  4494.     }
  4495.     
  4496.     if (ok)
  4497.     {
  4498.         switch ((*uaction).command)
  4499.         {
  4500.             case kODCommandCut:     somSelf->UndoRedoCut(ev, uaction, kUndoIt); break;
  4501.             case kODCommandPaste:    somSelf->UndoRedoPaste(ev, uaction, kUndoIt); break;
  4502.             case kODCommandPasteAs:    somSelf->UndoRedoPasteLink(ev, uaction, kUndoIt); break;
  4503.             case kODCommandClear:    somSelf->UndoRedoClear(ev, uaction, kUndoIt); break;
  4504.             case cDTSampleOne:        somSelf->UndoRedoSampleOne(ev, uaction, kUndoIt); break;
  4505.             case cDTSampleTwo:         somSelf->UndoRedoSampleTwo(ev, uaction, kUndoIt); break;
  4506.             case cDTTyping:         somSelf->UndoRedoTyping(ev, uaction, kUndoIt); break;
  4507.             case cDTStyling:         somSelf->UndoRedoStyling(ev, uaction, kUndoIt); break;
  4508.             default:
  4509.                 break;
  4510.         }
  4511.         
  4512.         _fUndoCount--;
  4513.         _fRedoCount++;
  4514.     }
  4515. #endif /* 0 */
  4516.  
  4517.     SOM_CATCH_ALL
  4518.     SOM_ENDTRY
  4519. }
  4520.  
  4521. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRedoAction(AppleTestDrag_DragText *somSelf, Environment *ev,
  4522.         ODActionData* actionState)
  4523. {
  4524.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4525.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RedoAction");
  4526.  
  4527.     SOM_TRY
  4528.  
  4529. #if 0
  4530.     WASSERTM((_fRedoCount != 0), "Redo unexpected, internal count is zero");
  4531.     
  4532.     ODBoolean ok = kODFalse;
  4533.     UndoInfo* uaction = (UndoInfo*) actionState;
  4534.     
  4535.     OrderedCollectionIterator dispIter(_fDisplayFrames);
  4536.     for (ODFrame* frame = (ODFrame*)dispIter.First();
  4537.          dispIter.IsNotComplete();
  4538.          frame = (ODFrame*)dispIter.Next())
  4539.     {
  4540.         if (frame == (*uaction).displayFrame) ok = kODTrue;
  4541.     }
  4542.     
  4543.     if (ok)
  4544.     {
  4545.         switch ((*uaction).command)
  4546.         {
  4547.             case kODCommandCut:     somSelf->UndoRedoCut(ev, uaction, kRedoIt); break;
  4548.             case kODCommandPaste:    somSelf->UndoRedoPaste(ev, uaction, kRedoIt); break;
  4549.             case kODCommandPasteAs:    somSelf->UndoRedoPasteLink(ev, uaction, kRedoIt); break;
  4550.             case kODCommandClear:    somSelf->UndoRedoClear(ev, uaction, kRedoIt); break;
  4551.             case cDTSampleOne:        somSelf->UndoRedoSampleOne(ev, uaction, kRedoIt); break;
  4552.             case cDTSampleTwo:         somSelf->UndoRedoSampleTwo(ev, uaction, kRedoIt); break;
  4553.             case cDTTyping:         somSelf->UndoRedoTyping(ev, uaction, kRedoIt); break;
  4554.             case cDTStyling:         somSelf->UndoRedoStyling(ev, uaction, kRedoIt); break;
  4555.             default:
  4556.                 break;
  4557.         }
  4558.         
  4559.         _fUndoCount++;
  4560.         _fRedoCount--;
  4561.     }
  4562. #endif /* 0 */
  4563.  
  4564.     SOM_CATCH_ALL
  4565.     SOM_ENDTRY
  4566. }
  4567.  
  4568. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDisposeActionState(AppleTestDrag_DragText *somSelf, Environment *ev,
  4569.         ODActionData* actionState,
  4570.         ODDoneState doneState)
  4571. {
  4572.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4573.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DisposeActionState");
  4574.  
  4575.     SOM_TRY
  4576.  
  4577.     // CC: The tests here should be logically inverted; but its not necessary
  4578.     //     that both of these counts be non zero.  So remove for now.
  4579.     // WASSERTM((_fUndoCount == 0), "Undo unexpected, internal count is zero");
  4580.     // WASSERTM((_fRedoCount == 0), "Redo unexpected, internal count is zero");
  4581. #if 0
  4582.     UndoInfo* uaction = (UndoInfo*) actionState;
  4583.     
  4584.     (*uaction).displayFrame->Release(ev);
  4585. //    if ((*uaction).text)
  4586. //        ODDisposePtr((Ptr)(*uaction).text);
  4587.     if ((*uaction).style)
  4588.         ODDisposeHandle((Handle) (*uaction).style);
  4589.     
  4590.     ODDisposePtr((ODPtr)uaction);
  4591.     
  4592.     switch (doneState) {
  4593.         case kODDone:
  4594.         case kODRedone:
  4595.             _fUndoCount--; break;
  4596.         case kODUndone:
  4597.             _fRedoCount--; break;
  4598.         default:
  4599.             ;
  4600.     }
  4601. #endif /* 0 */
  4602.  
  4603.     SOM_CATCH_ALL
  4604.     SOM_ENDTRY
  4605. }
  4606.  
  4607. SOM_Scope void  SOMLINK AppleTestDrag_DragTextWriteActionState(AppleTestDrag_DragText *somSelf, Environment *ev,
  4608.         ODActionData* actionState,
  4609.         ODStorageUnitView* storageUnitView)
  4610. {
  4611.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4612.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","WriteActionState");
  4613.  
  4614. ODUnused(actionState);
  4615. ODUnused(storageUnitView);
  4616. }
  4617.  
  4618. SOM_Scope ODActionData  SOMLINK AppleTestDrag_DragTextReadActionState(AppleTestDrag_DragText *somSelf, Environment *ev,
  4619.         ODStorageUnitView* storageUnitView)
  4620. {
  4621.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4622.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ReadActionState");
  4623.     
  4624.     ODActionData data;
  4625.     data._maximum = 0;
  4626.     data._length = 0;
  4627.     data._buffer = kODNULL;
  4628.     return ((ODActionData)data);
  4629. }
  4630.  
  4631. //-------------------------------------------------------------------------
  4632. // Utility Methods 
  4633. //-------------------------------------------------------------------------
  4634.  
  4635. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCommitRelinquishFocus(AppleTestDrag_DragText *somSelf, Environment *ev,
  4636.         ODTypeToken focus,
  4637.         ODFrame* ownerFrame,
  4638.         ODFrame* proposedFrame)
  4639. {
  4640.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4641.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CommitRelinquishFocus");
  4642.  
  4643. ODUnused(proposedFrame);
  4644.  
  4645.     SOM_TRY
  4646.  
  4647.     if (focus == _fSession->Tokenize(ev, kODMenuFocus)) 
  4648.     {
  4649.         somSelf->RemoveMenus(ev, ownerFrame);
  4650.         _fHasFoci = kODFalse;
  4651.     }
  4652.  
  4653.     SOM_CATCH_ALL
  4654.     SOM_ENDTRY
  4655. }
  4656.  
  4657. SOM_Scope void  SOMLINK AppleTestDrag_DragTextInstallMenus(AppleTestDrag_DragText *somSelf, Environment *ev,
  4658.         ODFrame* frame)
  4659. {
  4660.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4661.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InstallMenus");
  4662.  
  4663.     SOM_TRY
  4664.  
  4665.     _fMenuBar->Display(ev);
  4666.  
  4667.     ODFrameFacetIterator* fiter = frame->CreateFacetIterator(ev);
  4668.     somSelf->ClipThis(ev, fiter->First(ev));
  4669.     TEActivate(_fDocument->theTE);
  4670.  
  4671. #ifdef SHOW_SCROLL_BARS
  4672.     HiliteControl(_fDocument->vScroll, 0);
  4673.     HiliteControl(_fDocument->hScroll, 0);
  4674. #endif
  4675.  
  4676.     TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  4677.     _fDocument->frameIsActive = true;
  4678.     somSelf->ClipToRoot(ev, fiter->First(ev));
  4679.     ODDeleteObject(fiter);
  4680.  
  4681.     SOM_CATCH_ALL
  4682.     SOM_ENDTRY
  4683. }
  4684.  
  4685. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextUserCategoryString(AppleTestDrag_DragText *somSelf, Environment *ev,
  4686.         ODName* name)
  4687. {
  4688.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4689.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UserCategoryString");
  4690.  
  4691.     SOM_TRY
  4692.  
  4693.     ODBoolean result  = kODFalse ;
  4694.  
  4695.     ODNameSpaceManager* theNmSpcMgr = _fSession->GetNameSpaceManager(ev);
  4696.  
  4697.         // look it up in the spaceName namespace
  4698.     ODValueNameSpace* userStringNameSpace = 
  4699.         (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODCategoryUserString );
  4700.  
  4701.     if (userStringNameSpace)
  4702.         result = myValueNameSpaceGetODName(userStringNameSpace, ev, kODCategoryPlainText, &name );
  4703.     return result ;
  4704.  
  4705.     SOM_CATCH_ALL
  4706.     SOM_ENDTRY
  4707.     return kODFalse;
  4708. }
  4709.  
  4710. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRemoveMenus(AppleTestDrag_DragText *somSelf, Environment *ev,
  4711.         ODFrame* frame)
  4712. {
  4713.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4714.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RemoveMenus");
  4715.  
  4716.     SOM_TRY
  4717.  
  4718. #ifdef SHOW_TEXT_MENU
  4719.     DeleteMenu(140);
  4720. #endif
  4721.  
  4722.     DeleteMenu(141);
  4723.     DeleteMenu(142);
  4724.     
  4725.     InvalMenuBar();
  4726.     ODFrameFacetIterator* fiter = frame->CreateFacetIterator(ev);
  4727.     somSelf->ClipThis(ev, fiter->First(ev));
  4728.     TEDeactivate(_fDocument->theTE);
  4729.  
  4730. #ifdef SHOW_SCROLL_BARS
  4731.     HiliteControl(_fDocument->vScroll, 255);
  4732.     HiliteControl(_fDocument->hScroll, 255);
  4733. #endif
  4734.  
  4735.     _fDocument->frameIsActive = false;
  4736.     somSelf->ClipToRoot(ev, fiter->First(ev));
  4737.     ODDeleteObject(fiter);
  4738.  
  4739.     SOM_CATCH_ALL
  4740.     SOM_ENDTRY
  4741. }
  4742.  
  4743. SOM_Scope void  SOMLINK AppleTestDrag_DragTextExternalShapeToWindowShape(AppleTestDrag_DragText *somSelf, Environment *ev,
  4744.         ODFacet* facet,
  4745.         ODShape* inShape,
  4746.         ODShape* outShape)
  4747. {
  4748.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4749.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ExternalShapeToWindowShape");
  4750.  
  4751.     SOM_TRY
  4752.  
  4753.     ODTransform* localToGlobal = ODCopyAndRelease(ev, facet->AcquireWindowFrameTransform(ev, kODNULL));        
  4754.     // localToGlobal->Invert();
  4755.     outShape->CopyFrom(ev, inShape);
  4756.     outShape->Transform(ev, localToGlobal);
  4757.     ODReleaseObject(ev, localToGlobal);
  4758.  
  4759.     SOM_CATCH_ALL
  4760.     SOM_ENDTRY
  4761. }
  4762.  
  4763. SOM_Scope void  SOMLINK AppleTestDrag_DragTextActivatingWindow(AppleTestDrag_DragText *somSelf, Environment *ev,
  4764.         ODFrame* frame)
  4765. {
  4766.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4767.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ActivatingWindow");
  4768.  
  4769.     SOM_TRY
  4770.  
  4771.     if ( _fNeedsFoci )
  4772.     {
  4773.         somSelf->ActivateFrame(ev, frame);
  4774.         _fNeedsFoci = kODFalse;    // Should check if focus acquired?
  4775.     }
  4776.  
  4777.     if (_fDocument->frameIsActive)
  4778.     {
  4779.         ODFrameFacetIterator* fiter = frame->CreateFacetIterator(ev);
  4780.         somSelf->ClipThis(ev, fiter->First(ev));
  4781.  
  4782. #ifdef SHOW_LINK_BORDERS
  4783.         somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  4784. #endif
  4785.  
  4786.         // Since this part was active when its window was deactivated, 
  4787.         // turn off the inactive selection before activing the edit record.
  4788.         TEFeatureFlag(teFOutlineHilite, teBitClear, _fDocument->theTE);
  4789.         Rect hiliteRect = (**_fDocument->hiliteRgn).rgnBBox;
  4790.         TEUpdate(&hiliteRect, _fDocument->theTE);
  4791.  
  4792.         TEActivate(_fDocument->theTE);
  4793.  
  4794. #ifdef SHOW_SCROLL_BARS
  4795.         HiliteControl(_fDocument->vScroll, 0);
  4796.         HiliteControl(_fDocument->hScroll, 0);
  4797. #endif
  4798.  
  4799.         TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  4800.         somSelf->ClipToRoot(ev, fiter->First(ev));
  4801.         ODDeleteObject(fiter);
  4802.     }
  4803.  
  4804.     SOM_CATCH_ALL
  4805.     SOM_ENDTRY
  4806. }
  4807.  
  4808. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDeactivatingWindow(AppleTestDrag_DragText *somSelf, Environment *ev,
  4809.         ODFrame* frame)
  4810. {
  4811.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4812.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DeactivatingWindow");
  4813.  
  4814.     SOM_TRY
  4815.  
  4816.     if ( _fHasFoci )
  4817.         _fNeedsFoci = kODTrue;
  4818.     
  4819.     if (_fDocument->frameIsActive)
  4820.     {
  4821.         ODFrameFacetIterator* fiter = frame->CreateFacetIterator(ev);
  4822.         somSelf->ClipThis(ev, fiter->First(ev));
  4823.  
  4824. #ifdef SHOW_LINK_BORDERS
  4825.         somSelf->ShowLinkBorder(ev, _fDocument->theFrame, kLinkBorderOff);
  4826. #endif
  4827.  
  4828.         TEDeactivate(_fDocument->theTE);
  4829.  
  4830.         // Since this part was active when its window was deactivated, 
  4831.         // an inactive selection should appear.
  4832.         TEFeatureFlag(teFOutlineHilite, teBitSet, _fDocument->theTE);
  4833.         Rect hiliteRect = (**_fDocument->hiliteRgn).rgnBBox;
  4834.         TEUpdate(&hiliteRect, _fDocument->theTE);
  4835.  
  4836. #ifdef SHOW_SCROLL_BARS
  4837.         HiliteControl(_fDocument->vScroll, 255);
  4838.         HiliteControl(_fDocument->hScroll, 255);
  4839. #endif
  4840.  
  4841.         somSelf->ClipToRoot(ev, fiter->First(ev));
  4842.         ODDeleteObject(fiter);
  4843.     }
  4844.  
  4845.     SOM_CATCH_ALL
  4846.     SOM_ENDTRY
  4847. }
  4848.  
  4849. SOM_Scope void  SOMLINK AppleTestDrag_DragTextClipThis(AppleTestDrag_DragText *somSelf, Environment *ev,
  4850.         ODFacet* facet)
  4851. {
  4852.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4853.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ClipThis");
  4854.  
  4855.     SOM_TRY
  4856.  
  4857.     if ( !facet )
  4858.         PRINT("DragText %d: ClipThis: facet is null\n", somSelf->GetID(ev));
  4859.  
  4860.     ODWindow* window = facet->GetWindow(ev);
  4861.     SetPort(window->GetPlatformWindow(ev));
  4862.     
  4863.     Point xForm = GetAggregateTranslation(ev, facet).AsQDPoint();
  4864.     ODShape* clipShape = facet->AcquireClipShape(ev, kODNULL);
  4865.     RgnHandle clipRgn = clipShape->CopyQDRegion(ev);
  4866.  
  4867.     OffsetRgn(clipRgn, xForm.h, xForm.v);
  4868.     SetClip(clipRgn);
  4869.     ODReleaseObject(ev, clipShape);
  4870.     ODDisposeHandle((Handle)clipRgn);
  4871.  
  4872.     SOM_CATCH_ALL
  4873.     SOM_ENDTRY
  4874. }
  4875.  
  4876. SOM_Scope void  SOMLINK AppleTestDrag_DragTextClipToRoot(AppleTestDrag_DragText *somSelf, Environment *ev,
  4877.         ODFacet* facet)
  4878. {
  4879.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4880.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ClipToRoot");
  4881.  
  4882.     SOM_TRY
  4883.  
  4884.     if ( !facet )
  4885.         PRINT("DragText %d: ClipToRoot: facet is null\n", somSelf->GetID(ev));
  4886.  
  4887.     ODFacet* pointer = facet;
  4888.     while (pointer->GetContainingFacet(ev) != kODNULL)
  4889.         pointer = pointer->GetContainingFacet(ev);
  4890.     
  4891.     ODWindow* window = pointer->GetWindow(ev);
  4892.     SetPort(window->GetPlatformWindow(ev));
  4893.  
  4894.     ODShape* clipShape = pointer->AcquireClipShape(ev, kODNULL);
  4895.     SetClip(clipShape->GetQDRegion(ev));
  4896.     ODReleaseObject(ev, clipShape);
  4897.  
  4898.     SOM_CATCH_ALL
  4899.     SOM_ENDTRY
  4900. }
  4901.  
  4902. #if ODDebug
  4903. /*#define DEBUG_CYCLIC_LINK_UPDATES 1*/
  4904. #endif /*ODDebug*/
  4905.  
  4906. #ifdef DEBUG_CYCLIC_LINK_UPDATES
  4907.  
  4908. #include <time.h>
  4909. static time_t gIdleSeconds = 0;
  4910.  
  4911. #endif /*DEBUG_CYCLIC_LINK_UPDATES*/
  4912.  
  4913. SOM_Scope void  SOMLINK AppleTestDrag_DragTextIdle(AppleTestDrag_DragText *somSelf, Environment *ev,
  4914.         ODFrame* frame)
  4915. {
  4916.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4917.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","Idle");
  4918.  
  4919.     SOM_TRY
  4920.  
  4921. #ifdef DEBUG_CYCLIC_LINK_UPDATES
  4922.     time_t idleSeconds;
  4923.     time(&idleSeconds);
  4924.     if ( !gIdleSeconds )
  4925.         gIdleSeconds = idleSeconds;
  4926.         
  4927.     if ( (idleSeconds - 6) > gIdleSeconds && _fSrcLink.linkSource )
  4928.     {
  4929.         gIdleSeconds = idleSeconds;
  4930.         ODLinkKey key;
  4931.         if (_fSrcLink.linkSource->Lock(ev, 0, &key)) {
  4932.             _fSrcLink.linkSource->ContentUpdated(ev, 0x7654, key);
  4933.             _fSrcLink.linkSource->Unlock(ev, key);
  4934.         }
  4935.     }
  4936. #endif /*DEBUG_CYCLIC_LINK_UPDATES*/
  4937.  
  4938.     if ( _fDocument && (_fDocument->theFrame == frame) )
  4939.     {
  4940.         ODFrameFacetIterator* fiter = frame->CreateFacetIterator(ev);
  4941.  
  4942.         if ( fiter->First(ev) )
  4943.         {
  4944.             somSelf->ClipThis(ev, fiter->First(ev));
  4945.     
  4946. #ifdef SHOW_LINK_BORDERS
  4947.             EventRecord dummyEvent;
  4948.             TempODWindow    window = frame->AcquireWindow(ev);
  4949.             ODBoolean        showLinks = window->ShouldShowLinks(ev);
  4950.             
  4951.             // Show the borders, don't do it if keystroke in buffer
  4952.             if ( (_fLinkBorderStyle == kLinkBorderOff) && ((_fTimeBorderOff+20) < TickCount())
  4953.                 && !EventAvail(keyDownMask, &dummyEvent) && showLinks)
  4954.             {
  4955.                 // Turn off the selection, draw the border, turn the selection back on
  4956.                 ODSShort selStart = (**(_fDocument->theTE)).selStart;
  4957.                 ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  4958.                 
  4959.                 TESetSelect(selStart, selStart, _fDocument->theTE);
  4960.  
  4961.                 somSelf->ShowLinkBorder(ev, frame, kLinkBorderOn);
  4962.  
  4963.                 TESetSelect(selStart, selEnd, _fDocument->theTE);
  4964.             }
  4965.             if (!showLinks)
  4966.                 somSelf->ShowLinkBorder(ev, frame, kLinkBorderOff);
  4967. #endif
  4968.     
  4969.             if ( _fDocument->frameIsActive )
  4970.                 TEIdle(_fDocument->theTE);
  4971.             
  4972.             somSelf->ClipToRoot(ev, fiter->First(ev));
  4973.         }
  4974.         else
  4975.         {
  4976.             // $$$$$ MacTech CD: Since this frame has no facets, unregister for idle time.
  4977.             // When a frame is moved across a part boundary, a new part is created, but
  4978.             // the old part does not recieve a DisplayFrameRemoved message.
  4979.             // $$$$$ Talk to Joshua about what to do post-MacTech.
  4980.             PRINT("DragText %d: Idle: Unregistering for Idle events\n", somSelf->GetID(ev));
  4981.             //_fSession->GetDispatcher(ev)->UnregisterIdle(ev, _fPartWrapper, frame);
  4982.         }
  4983.  
  4984.         ODDeleteObject(fiter);
  4985.     }
  4986.  
  4987.     SOM_CATCH_ALL
  4988.     SOM_ENDTRY
  4989. }
  4990.  
  4991. //SOM_Scope void  SOMLINK InvalidateFacet(AppleTestDrag_DragText *somSelf, Environment *ev,
  4992. //        ODFacet* facet)
  4993. //{
  4994. //    AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  4995. //    AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InvalidateFacet");
  4996. //
  4997. //    facet->Invalidate(ev, NULL);
  4998. //}
  4999.  
  5000. SOM_Scope void  SOMLINK AppleTestDrag_DragTextFrameShapeChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  5001.         ODFrame* frame)
  5002. {
  5003.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5004.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","FrameShapeChanged");
  5005.  
  5006.     SOM_TRY
  5007.  
  5008.     frame->Invalidate(ev, kODNULL, kODNULL);
  5009.         
  5010.     somSelf->PositionDocumentParts(ev);
  5011.     somSelf->AdjustScrollBar(ev);
  5012.     TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  5013.     _fFrameShapeChanged = false;
  5014.  
  5015.     SOM_CATCH_ALL
  5016.     SOM_ENDTRY
  5017. }
  5018.  
  5019. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextActivateFrame(AppleTestDrag_DragText *somSelf, Environment *ev,
  5020.         ODFrame* frame)
  5021. {
  5022.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5023.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ActivateFrame");
  5024.  
  5025.     SOM_TRY
  5026.  
  5027.     if (!_fHasFoci)
  5028.     {
  5029.         ODBoolean succeeded = false;
  5030.                             
  5031.         succeeded = _fSession->GetArbitrator(ev)->RequestFocusSet(ev, _fFocusSet,frame);
  5032.         
  5033.         if (succeeded)
  5034.         {
  5035.             somSelf->InstallMenus(ev, frame);
  5036.             _fHasFoci = true;
  5037.         }
  5038.     }
  5039.     return true;
  5040.  
  5041.     SOM_CATCH_ALL
  5042.     SOM_ENDTRY
  5043.     return kODTrue;
  5044. }
  5045.  
  5046. SOM_Scope void  SOMLINK AppleTestDrag_DragTextDeactivateFrame(AppleTestDrag_DragText *somSelf, Environment *ev,
  5047.         ODFrame* frame)
  5048. {
  5049.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5050.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DeactivateFrame");
  5051.  
  5052.     SOM_TRY
  5053.  
  5054.     ODArbitrator* arbitrator = _fSession->GetArbitrator(ev);
  5055.  
  5056.     if ( _fHasFoci )
  5057.     {
  5058.         arbitrator->RelinquishFocusSet(ev, _fFocusSet, frame);
  5059.         _fHasFoci = kODFalse;
  5060.     }
  5061.  
  5062.     arbitrator->RelinquishFocus(ev, _fClipboardFocus, frame);
  5063.  
  5064.     SOM_CATCH_ALL
  5065.     SOM_ENDTRY
  5066. }
  5067.  
  5068. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustFontMenu(AppleTestDrag_DragText *somSelf, Environment *ev)
  5069. {
  5070.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5071.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustFontMenu");
  5072.  
  5073.     SOM_TRY
  5074.  
  5075.     MenuHandle fontMenuH = _fMenuBar->GetMenu(ev, 141);
  5076.  
  5077.     if ( fontMenuH )
  5078.     {
  5079.         Str255 fontName;
  5080.     
  5081.         if ( _fLastFont != -1 )
  5082.         {
  5083.             GetFontName( _fLastFont, fontName );
  5084.             CheckMarkMenu(fontMenuH, fontName, kODFalse );
  5085.         }
  5086.         
  5087.         StScrpHandle stylHandle = TEGetStyleScrapHandle(_fDocument->theTE);
  5088.         
  5089.         if ( (**stylHandle).scrpNStyles == 1 )
  5090.         {
  5091.             _fLastFont = (**stylHandle).scrpStyleTab[0].scrpFont;
  5092.             GetFontName( _fLastFont, fontName );
  5093.             CheckMarkMenu(fontMenuH, fontName, kODTrue );
  5094.         }
  5095.         else
  5096.         {
  5097.             _fLastFont = -1;
  5098.         }
  5099.         
  5100.         ODDisposeHandle((Handle)stylHandle);
  5101.     }
  5102.  
  5103.     SOM_CATCH_ALL
  5104.     SOM_ENDTRY
  5105. }
  5106.  
  5107. SOM_Scope void  SOMLINK AppleTestDrag_DragTextHandleFontMenu(AppleTestDrag_DragText *somSelf, Environment *ev,
  5108.         ODUShort item)
  5109. {
  5110.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5111.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleFontMenu");
  5112.  
  5113.     SOM_TRY
  5114.  
  5115.     Str255        fontName;
  5116.     ODSShort    fontNum;
  5117.     
  5118.     MenuHandle fontMenuH = _fMenuBar->GetMenu(ev, 141);
  5119.  
  5120.     GetMenuItemText( fontMenuH, item, fontName );
  5121.     
  5122.     GetFNum( fontName, &fontNum );
  5123.     
  5124.     SetTextFont(_fDocument->theTE, fontNum);
  5125.  
  5126.     TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  5127.  
  5128.     // If any text was affected, mark the draft as changed
  5129.     ODSShort selStart = (**(_fDocument->theTE)).selStart;
  5130.     ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  5131.     if (selEnd > selStart) {
  5132.         _fDirty = kODTrue;
  5133.         _fDraft->SetChangedFromPrev(ev);
  5134.     }
  5135.  
  5136.     // Update any affected links
  5137.     somSelf->AdjustLinks(ev, selStart, selEnd, selEnd, _fSession->UniqueUpdateID(ev));
  5138.  
  5139.     SOM_CATCH_ALL
  5140.     SOM_ENDTRY
  5141. }
  5142.  
  5143. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustSizeMenu(AppleTestDrag_DragText *somSelf, Environment *ev)
  5144. {
  5145.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5146.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustSizeMenu");
  5147.  
  5148.     SOM_TRY
  5149.  
  5150.     MenuHandle sizeMenuH = _fMenuBar->GetMenu(ev, 142);
  5151.     
  5152.     if ( sizeMenuH )
  5153.     {
  5154.         Str255 fontSizeStr;
  5155.  
  5156.         if ( _fLastSize != 0 )
  5157.         {
  5158.             NumToString((ODULong) _fLastSize, fontSizeStr);
  5159.             CheckMarkMenu(sizeMenuH, fontSizeStr, kODFalse);
  5160.         }
  5161.         
  5162.         StScrpHandle stylHandle = TEGetStyleScrapHandle(_fDocument->theTE);
  5163.         
  5164.         if ( (**stylHandle).scrpNStyles == 1 )
  5165.         {
  5166.             _fLastSize = (**stylHandle).scrpStyleTab[0].scrpSize;
  5167.             switch (_fLastSize)
  5168.             {
  5169.                 case 0:
  5170.                     _fLastSize = 12;
  5171.                 case 9:
  5172.                 case 10:
  5173.                 case 11:
  5174.                 case 12:
  5175.                 case 14:
  5176.                 case 18:
  5177.                 case 24:
  5178.                 case 36:
  5179.                     NumToString((ODULong) _fLastSize, fontSizeStr);
  5180.                     CheckMarkMenu(sizeMenuH, fontSizeStr, kODTrue);
  5181.                     break;
  5182.                 default:
  5183.                     _fLastSize = 0;
  5184.             }
  5185.             somSelf->OutlineSizeMenu(ev, (**stylHandle).scrpStyleTab[0].scrpFont);
  5186.         }
  5187.         else
  5188.         {
  5189.             _fLastSize = 0;
  5190.         }
  5191.         
  5192.         ODDisposeHandle((Handle)stylHandle);
  5193.     }
  5194.  
  5195.     SOM_CATCH_ALL
  5196.     SOM_ENDTRY
  5197. }
  5198.  
  5199. SOM_Scope void  SOMLINK AppleTestDrag_DragTextHandleSizeMenu(AppleTestDrag_DragText *somSelf, Environment *ev,
  5200.         ODUShort item)
  5201. {
  5202.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5203.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HandleSizeMenu");
  5204.  
  5205.     SOM_TRY
  5206.  
  5207.     ODSShort fontSize = 0;
  5208.     
  5209.     switch (item)
  5210.     {
  5211.         case 1:    fontSize =  9; break;
  5212.         case 2:    fontSize = 10; break;
  5213.         case 3:    fontSize = 11; break;
  5214.         case 4:    fontSize = 12; break;
  5215.         case 5:    fontSize = 14; break;
  5216.         case 6:    fontSize = 18; break;
  5217.         case 7:    fontSize = 24; break;
  5218.         case 8:    fontSize = 36; break;
  5219.         default:
  5220.             fontSize = 12;
  5221.     }
  5222.     
  5223.     SetTextSize(_fDocument->theTE, fontSize);
  5224.  
  5225.     TEGetHiliteRgn(_fDocument->hiliteRgn, _fDocument->theTE);
  5226.  
  5227.     // If any text was affected, mark the draft as changed
  5228.     ODSShort selStart = (**(_fDocument->theTE)).selStart;
  5229.     ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  5230.     if (selEnd > selStart) {
  5231.         _fDirty = kODTrue;
  5232.         _fDraft->SetChangedFromPrev(ev);
  5233.     }
  5234.  
  5235.     // Update any affected links
  5236.     somSelf->AdjustLinks(ev, selStart, selEnd, selEnd, _fSession->UniqueUpdateID(ev));
  5237.  
  5238.     SOM_CATCH_ALL
  5239.     SOM_ENDTRY
  5240. }
  5241.  
  5242. SOM_Scope void  SOMLINK AppleTestDrag_DragTextOutlineSizeMenu(AppleTestDrag_DragText *somSelf, Environment *ev,
  5243.     ODSShort fontNum)
  5244. {
  5245.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5246.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","OutlineSizeMenu");
  5247.  
  5248.     SOM_TRY
  5249.  
  5250.     MenuHandle menu = _fMenuBar->GetMenu(ev, 142);
  5251.     
  5252.     // (•) Outline the sizes in the font menu that are available
  5253.     
  5254.     if ((fontNum != -1) && RealFont(fontNum, 9))
  5255.         SetItemStyle( menu, 1, outline );
  5256.     else
  5257.         SetItemStyle( menu, 1, normal );
  5258.  
  5259.     if ((fontNum != -1) && RealFont(fontNum, 10))
  5260.         SetItemStyle( menu, 2, outline );
  5261.     else
  5262.         SetItemStyle( menu, 2, normal );
  5263.  
  5264.     if ((fontNum != -1) && RealFont(fontNum, 11))
  5265.         SetItemStyle( menu, 3, outline );
  5266.     else
  5267.         SetItemStyle( menu, 3, normal );
  5268.  
  5269.     if ((fontNum != -1) && RealFont(fontNum, 12))
  5270.         SetItemStyle( menu, 4, outline );
  5271.     else
  5272.         SetItemStyle( menu, 4, normal );
  5273.  
  5274.     if ((fontNum != -1) && RealFont(fontNum, 14))
  5275.         SetItemStyle( menu, 5, outline );
  5276.     else
  5277.         SetItemStyle( menu, 5, normal );
  5278.  
  5279.     if ((fontNum != -1) && RealFont(fontNum, 18))
  5280.         SetItemStyle( menu, 6, outline );
  5281.     else
  5282.         SetItemStyle( menu, 6, normal );
  5283.  
  5284.     if ((fontNum != -1) && RealFont(fontNum, 24))
  5285.         SetItemStyle( menu, 7, outline );
  5286.     else
  5287.         SetItemStyle( menu, 7, normal );
  5288.  
  5289.     if ((fontNum != -1) && RealFont(fontNum, 36))
  5290.         SetItemStyle( menu, 8, outline );
  5291.     else
  5292.         SetItemStyle( menu, 8, normal );
  5293.  
  5294.     SOM_CATCH_ALL
  5295.     SOM_ENDTRY
  5296. }
  5297.  
  5298. //------------------------------------------------------------------------------
  5299. // DragText: Open
  5300. //------------------------------------------------------------------------------
  5301. SOM_Scope ODID  SOMLINK AppleTestDrag_DragTextOpen(AppleTestDrag_DragText *somSelf, Environment *ev,
  5302.         ODFrame* frame)
  5303. {
  5304.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5305.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","Open");
  5306.  
  5307.     SOM_TRY
  5308.  
  5309.     ODWindow* window = kODNULL;
  5310.     ODID windowID = kODNULLID;
  5311.  
  5312.     if (frame) // Doing a View In Window or Open Root
  5313.     {
  5314.         if (frame->IsRoot(ev))    // Create Window For Root Frame
  5315.         {
  5316.             WindowProperties props;
  5317.             if ( BeginGetWindowProperties(ev, frame, &props) )
  5318.             {
  5319.                 ODPlatformWindow platformWindow = NewCWindow(kODNULL, &(props.boundsRect), props.title, 
  5320.                             kODFalse, props.procID, (WindowPtr)-1L, props.hasCloseBox, props.refCon);
  5321.                 
  5322.                         
  5323.                 window =  _fSession->GetWindowState(ev)->RegisterWindowForFrame(ev, platformWindow, 
  5324.                                                                 frame,
  5325.                                                                  props.isRootWindow,    // Keeps draft open
  5326.                                                                  kODTrue,    // Is resizable
  5327.                                                                  kODFalse,    // Is floating
  5328.                                                                 kODTrue,    // should save
  5329.                                                                 kODTrue,    // should dispose
  5330.                                                                 props.sourceFrame);
  5331.                 EndGetWindowProperties(ev, &props); // Release source frame
  5332.                 window->Open(ev);
  5333.             }
  5334.             else
  5335.             {
  5336.                 Rect windRect;
  5337.                 SetRect(&windRect, 100, 100, 300, 300);    
  5338.                 ODPlatformWindow platformWindow = NewCWindow(kODNULL, &windRect, "\p", kODFalse, zoomDocProc, (WindowPtr)-1L, true, kODNULL);
  5339.                         
  5340.                 window =  _fSession->GetWindowState(ev)->RegisterWindowForFrame(ev, platformWindow, 
  5341.                                                                 frame,
  5342.                                                                  kODTrue,    // Keeps draft open
  5343.                                                                  kODTrue,    // Is resizable
  5344.                                                                  kODFalse,    // Is floating
  5345.                                                                 kODTrue,    // should save
  5346.                                                                 kODTrue,    // should dispose
  5347.                                                                 kODNULL);
  5348.                 window->Open(ev);    
  5349.                 window->AdjustWindowShape(ev);
  5350.             }
  5351.             window->Show(ev);
  5352.         }
  5353.         else // View In Window
  5354.         {
  5355.             window = _fSession->GetWindowState(ev)->AcquireWindow(ev, _fWindowID);
  5356.             if (window)
  5357.             {
  5358.                 window->Select(ev);
  5359.             }
  5360.             else
  5361.             {
  5362.                 window = somSelf->CreateWindow(ev, frame);
  5363.                 _fWindowID = window->GetID(ev);
  5364.                 window->Open(ev);
  5365.                 window->Show(ev);
  5366.                 window->Select(ev);            
  5367.             }
  5368.         }
  5369.     }
  5370.     else
  5371.     {
  5372.         window = somSelf->CreateWindow(ev, frame);
  5373.         _fWindowID = window->GetID(ev);
  5374.         window->Open(ev);
  5375.         window->Show(ev);
  5376.         window->Select(ev);            
  5377.     }
  5378.     windowID = window->GetID(ev);
  5379.     ODReleaseObject(ev, window); // Because we aren't hanging on to it
  5380.     return windowID;
  5381.  
  5382.     SOM_CATCH_ALL
  5383.     SOM_ENDTRY
  5384.     return kODNULLID;
  5385. }
  5386.  
  5387. SOM_Scope ODWindow*  SOMLINK AppleTestDrag_DragTextCreateWindow(AppleTestDrag_DragText *somSelf, Environment *ev,
  5388.         ODFrame* sourceFrame)
  5389. {
  5390.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5391.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CreateWindow");
  5392.  
  5393.     SOM_TRY
  5394.  
  5395.     Rect windRect;
  5396.     ODPlatformWindow platformWindow = kODNULL;
  5397.     ODWindow* window = kODNULL;
  5398.     
  5399.     if (sourceFrame)
  5400.     {
  5401.         SetRect(&windRect, 100, 100, 300, 300);    
  5402.     }
  5403.     else
  5404.     {
  5405.         const    ODSShort        kOnePageWidth = 600;
  5406.         // Get the menu bar height, for figuring the top of our windows
  5407.         ODSShort    mbHeight    = 24; //(*(ODSShort*)MBarHeight);    //get it from lomem
  5408.         SetRect(&windRect, 4, mbHeight+16,
  5409.                 ODQDGlobals.screenBits.bounds.right - 64,
  5410.                 ODQDGlobals.screenBits.bounds.bottom - 4);        
  5411.         
  5412.         if (windRect.right-windRect.left > kOnePageWidth)
  5413.         {
  5414.             windRect.right = windRect.left + kOnePageWidth;
  5415.         }
  5416.     }
  5417.     
  5418.     ODName* windowName = kODNULL;
  5419.     Str255  pWindowName;
  5420.     if (sourceFrame)
  5421.         windowName = ODGetPartName(ev, sourceFrame, kODNULL);
  5422.     if (windowName==kODNULL || GetITextStringLength(windowName)==0)
  5423.         CopyPascalString(pWindowName, "\pDragText");
  5424.     else
  5425.         GetITextPString(windowName, pWindowName);
  5426.         
  5427.     if (windowName)
  5428.         DisposeIText(windowName);
  5429.  
  5430.     platformWindow = NewCWindow(kODNULL, &windRect, pWindowName, false, zoomDocProc, (WindowPtr)-1L, true, kODNULL);
  5431.     window =  _fSession->GetWindowState(ev)->RegisterWindow(ev, platformWindow, 
  5432.                                                     kODFrameObject,
  5433.                                                     (sourceFrame==kODNULL),    // Keeps draft open
  5434.                                                     kODTrue,    // Is resizable
  5435.                                                     kODFalse,    // Is floating
  5436.                                                     kODTrue,    // should save
  5437.                                                     kODTrue,    // should dispose
  5438.                                                     _fPartWrapper, _fSession->Tokenize(ev, kODViewAsFrame),
  5439.                                                     _fSession->Tokenize(ev, kODViewAsFrame),
  5440.                                                     sourceFrame);
  5441.  
  5442.     return window;
  5443.  
  5444.     SOM_CATCH_ALL
  5445.     SOM_ENDTRY
  5446.     return kODNULL;
  5447. }
  5448.  
  5449.  
  5450.  
  5451.  
  5452. //••••••••••••••••••••••••••••••••••
  5453. // Utilities
  5454. //••••••••••••••••••••••••••••••••••
  5455.  
  5456. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCreateDocument(AppleTestDrag_DragText *somSelf, Environment *ev,
  5457.         ODFrame* frame)
  5458. {
  5459.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5460.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CreateDocument");
  5461.  
  5462.     SOM_TRY
  5463.  
  5464.     TEStyleHandle theStyleHandle;
  5465.     GrafPtr    savePort;
  5466.     ODShape* frameShape = kODNULL;
  5467.     RgnHandle FrameRgn;
  5468.     Point xForm;
  5469.     Rect destRect, viewRect, FrameBounds;
  5470.  
  5471. #ifdef SHOW_SCROLL_BARS
  5472.     Rect scrollRect;
  5473. #endif
  5474.     
  5475.     // For now we can support only one display frame so if _fDocument is already
  5476.     // present, then the display frame must have been changed.
  5477.     if (_fDocument)
  5478.     {
  5479.         _fDocument->theFrame = frame;
  5480.         _fFrameShapeChanged = true;
  5481.     }
  5482.     else
  5483.     {
  5484.         ODFrameFacetIterator* faceti = frame->CreateFacetIterator(ev);
  5485.  
  5486. #if ODDebug
  5487.         if ( !faceti->First(ev) )
  5488.             PRINT("DragText %d: CreateDocument: facet is null\n", somSelf->GetID(ev));
  5489. #endif
  5490.  
  5491.         ODWindow* window = faceti->First(ev)->GetWindow(ev);
  5492.         
  5493.         _fDocument = (Document *) ODNewPtrClear(sizeof(Document), kDefaultHeapID);
  5494.         _fDocument->theWindow = window->GetPlatformWindow(ev);
  5495.         _fDocument->theFrame = frame;
  5496.         
  5497.         frame->SetDroppable(ev, kODTrue);
  5498.     
  5499.         GetPort(&savePort);
  5500.         SetPort(_fDocument->theWindow);
  5501.         short saveTxSize = _fDocument->theWindow->txSize;
  5502.         short saveTxFont = _fDocument->theWindow->txFont;
  5503.         short saveTxFace = _fDocument->theWindow->txFace;
  5504.         SetOrigin(0, 0);
  5505.             
  5506.         frameShape = _fDocument->theFrame->AcquireFrameShape(ev, kODNULL);
  5507.         FrameRgn = frameShape->GetQDRegion(ev);
  5508.         ODReleaseObject(ev, frameShape);
  5509.         xForm = GetAggregateTranslation(ev, faceti->First(ev)).AsQDPoint();
  5510.         FrameBounds = (**FrameRgn).rgnBBox;
  5511.         OffsetRect(&FrameBounds, xForm.h, xForm.v);
  5512.         
  5513. #ifdef SHOW_SCROLL_BARS
  5514.         scrollRect = FrameBounds;
  5515.         scrollRect.left = scrollRect.right - 16;
  5516.         scrollRect.bottom -= 15;
  5517.         _fDocument->vScroll = NewControl(_fDocument->theWindow, &scrollRect, (ConstStr255Param)"", true, 0, 0, 0, 
  5518.                                 scrollBarProc, (long)_fDocument);
  5519.         
  5520.         scrollRect = FrameBounds;
  5521.         scrollRect.top = scrollRect.bottom - 16;
  5522.         scrollRect.right  -= 15;
  5523.         _fDocument->hScroll = NewControl(_fDocument->theWindow, &scrollRect, (ConstStr255Param)"", true, 0, 0, 0,
  5524.                                 scrollBarProc, (long)_fDocument);
  5525. #else
  5526.         _fDocument->vScroll = (ControlHandle) -1;    // Force address error if dereferenced
  5527.         _fDocument->hScroll = (ControlHandle) -1;
  5528. #endif
  5529.         
  5530.         viewRect = FrameBounds;
  5531. #ifdef SHOW_SCROLL_BARS
  5532.         viewRect.right  -= 16;
  5533.         viewRect.bottom -= 16;
  5534. #endif
  5535.         
  5536.         destRect = viewRect;
  5537.         destRect.top    += TopMargin;
  5538.         destRect.left   += LeftMargin;
  5539.         destRect.bottom  = 32767;
  5540.         destRect.right  -= RightMargin;
  5541.         
  5542.         // Before calling TEStyleNew, set the port's text attributes to the desired default
  5543.         // values in case _fTextHandle is null
  5544.         TextSize(defaultFontSize);
  5545.         TextFont(defaultFontNum);
  5546.         TextFace(defaultFontFace);
  5547.  
  5548.         _fDocument->theTE = TEStyleNew(&destRect, &viewRect);
  5549.         
  5550.         TEAutoView(true, _fDocument->theTE);
  5551.         
  5552.         // $$$$$ TEFeatureFlag(teFOutlineHilite, TEBitSet, _fDocument->theTE);
  5553.  
  5554.         if (_fTextHandle)
  5555.         {
  5556.             TEActivate(_fDocument->theTE);
  5557.             TESetSelect(32767, 32767, _fDocument->theTE);
  5558.             ODLockHandle((ODHandle) _fTextHandle);
  5559.             TEStyleInsert(*_fTextHandle, ODGetHandleSize((ODHandle) _fTextHandle), _fStylHandle, _fDocument->theTE);
  5560.             ODUnlockHandle((ODHandle) _fTextHandle);
  5561.             TECalText(_fDocument->theTE);
  5562.             TEDeactivate(_fDocument->theTE);
  5563.         }
  5564.  
  5565.         ODDisposeHandle((Handle) _fTextHandle);
  5566.         _fTextHandle = kODNULL;
  5567.         ODDisposeHandle((Handle) _fStylHandle);
  5568.         _fStylHandle = kODNULL;
  5569.  
  5570.         _fDocument->hiliteRgn = ODNewRgn();
  5571.         theStyleHandle = TEGetStyleHandle(_fDocument->theTE);
  5572.         (**theStyleHandle).teRefCon = (long)_fDocument;
  5573.         
  5574.         _fFrameShapeChanged = true;
  5575.     
  5576.         TextSize(saveTxSize);
  5577.         TextFont(saveTxFont);
  5578.         TextFace(saveTxFace);
  5579.         SetPort(savePort);
  5580.  
  5581.         ODDeleteObject(faceti);
  5582.         
  5583.         // If a destination link was created by Internalize, we can register it now
  5584.         if ( (_fDstLink.link != (ODLink*) kODNULL) && _fDstLink.info.autoUpdate )
  5585.         {
  5586.             TRY
  5587.                 _fDstLink.link->RegisterDependent(ev, _fPartWrapper, _fDstLink.info.change);
  5588.             CATCH_ALL
  5589.                 if( ErrorCode()==kODErrBrokenLink ) {
  5590.                     PRINT("DragText: ODLink::RegisterDependent() returned broken link error\n");
  5591.                     somSelf->RemoveDstLink(ev);
  5592.                 }
  5593.             ENDTRY
  5594.         }
  5595.     }
  5596.  
  5597.     SOM_CATCH_ALL
  5598.     SOM_ENDTRY
  5599. }
  5600.  
  5601. /*
  5602.  *    This routine computes the size and location of the window's scroll bars
  5603.  *    and the TextEdit field given the current size of the window. This routine
  5604.  *    is called to initialize these components of the window and to update
  5605.  *    the position of these components when the window is resized.
  5606.  */
  5607. SOM_Scope void  SOMLINK AppleTestDrag_DragTextPositionDocumentParts(AppleTestDrag_DragText *somSelf, Environment *ev)
  5608. {
  5609.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5610.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","PositionDocumentParts");
  5611.  
  5612.     SOM_TRY
  5613.  
  5614.     ODShape* frameShape = kODNULL;
  5615.     RgnHandle FrameRgn;
  5616.     Point xForm;
  5617.     Rect theRect, FrameBounds;
  5618.     GrafPtr    savePort;
  5619.     
  5620.     GetPort(&savePort);
  5621.     SetPort(_fDocument->theWindow);
  5622.     SetOrigin(0, 0);
  5623.     
  5624.     {
  5625.         TempODFrameFacetIterator faceti(ev,_fDocument->theFrame);
  5626.         xForm = GetAggregateTranslation(ev, faceti.Current()).AsQDPoint();
  5627.     }
  5628.     frameShape = _fDocument->theFrame->AcquireFrameShape(ev, kODNULL);
  5629.     FrameRgn = frameShape->GetQDRegion(ev);
  5630.     ODReleaseObject(ev, frameShape);
  5631.     FrameBounds = (**FrameRgn).rgnBBox;
  5632.     OffsetRect(&FrameBounds, xForm.h, xForm.v);
  5633.  
  5634. #ifdef SHOW_SCROLL_BARS
  5635.     theRect = FrameBounds;
  5636.     theRect.left = theRect.right - 16;
  5637.     theRect.bottom -= 15;
  5638.     (**(_fDocument->vScroll)).contrlRect = theRect;
  5639.  
  5640.     theRect = FrameBounds;
  5641.     theRect.top = theRect.bottom - 16;
  5642.     theRect.right  -= 15;
  5643.     (**(_fDocument->hScroll)).contrlRect = theRect;
  5644. #endif
  5645.  
  5646.     theRect = FrameBounds;
  5647.     
  5648.     // Allow two pixels for drag text frame
  5649.     InsetRect(&theRect, 2, 2);
  5650.  
  5651. #ifdef SHOW_SCROLL_BARS
  5652.     theRect.right  -= 16;
  5653.     theRect.bottom -= 16;
  5654. #endif
  5655.     (**(_fDocument->theTE)).viewRect = theRect;
  5656.     (**(_fDocument->theTE)).destRect.right = theRect.right - RightMargin;
  5657.     (**(_fDocument->theTE)).destRect.top   = theRect.top + TopMargin;
  5658.     (**(_fDocument->theTE)).destRect.left  = theRect.left + LeftMargin;
  5659.     TECalText(_fDocument->theTE);
  5660.  
  5661.     SetPort(savePort);
  5662.  
  5663.     SOM_CATCH_ALL
  5664.     SOM_ENDTRY
  5665. }
  5666.  
  5667. /*
  5668.  *    AdjustScrollBar calculates the current position and maximum value of the
  5669.  *    document's scroll bar given the current viewing position and document
  5670.  *    text. This routine is called whenever the text is changed or the view
  5671.  *    is auto-scrolled.
  5672.  */
  5673. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustScrollBar(AppleTestDrag_DragText *somSelf, Environment *ev)
  5674. {
  5675.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5676.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustScrollBar");
  5677.  
  5678.     SOM_TRY
  5679.  
  5680. #ifdef SHOW_SCROLL_BARS
  5681.     short        docTop, docBottom, viewTop, viewBottom;
  5682.     short        offTop, offBottom;
  5683.     RgnHandle    viewRgn;
  5684.  
  5685.     docTop = (**(_fDocument->theTE)).destRect.top;
  5686.     docBottom = (short)(docTop + TEGetHeight(32767, 0, _fDocument->theTE));
  5687.     viewTop = (**(_fDocument->theTE)).viewRect.top;
  5688.     viewBottom = (**(_fDocument->theTE)).viewRect.bottom;
  5689.  
  5690.     offTop = ((viewTop - (docTop - TopMargin)) + ScrollResolution - 1) / ScrollResolution;
  5691.     offBottom = (((docBottom + BottomMargin) - viewBottom) + ScrollResolution - 1) / ScrollResolution;
  5692.     if (offTop < 0)
  5693.         offTop = 0;
  5694.     if (offBottom < 0)
  5695.         offBottom = 0;
  5696.  
  5697.     _fDocument->vScrollPos = offTop;
  5698.  
  5699.     SetCtlMax(_fDocument->vScroll, offTop + offBottom);
  5700.     SetCtlValue(_fDocument->vScroll, offTop);
  5701.  
  5702.     viewRgn = ODNewRgn();
  5703.     RectRgn(viewRgn, &(**(_fDocument->theTE)).viewRect);
  5704.     SectRgn(viewRgn, _fDocument->hiliteRgn, _fDocument->hiliteRgn);
  5705.     ODDisposeHandle((Handle)viewRgn);
  5706. #endif
  5707.  
  5708.     SOM_CATCH_ALL
  5709.     SOM_ENDTRY
  5710. }
  5711.  
  5712. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustCursor(AppleTestDrag_DragText *somSelf, Environment *ev,
  5713.         ODPoint* where)
  5714. {
  5715.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5716.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustCursor");
  5717.  
  5718.     SOM_TRY
  5719.  
  5720.     RgnHandle    arrowRgn, iBeamRgn, hiliteRgn;
  5721.     Rect        theRect;
  5722.     Point        thePoint,theLoc;
  5723.     GrafPtr        savePort;
  5724.     
  5725.     GetPort(&savePort);
  5726.     SetPort(_fDocument->theWindow);
  5727.     SetOrigin(0, 0);
  5728.  
  5729.     theLoc = where->AsQDPoint();
  5730. #if ODDebug && ODDebugCursor
  5731.     PRINT("AdjustCursor Local H: %d V: %d\n",theLoc.h,theLoc.v);
  5732. #endif
  5733.     LocalToGlobal(&theLoc);
  5734.     
  5735.     arrowRgn = ODNewRgn();
  5736.     SetRectRgn(arrowRgn, -32767, -32767, 32767, 32767);
  5737.     iBeamRgn = ODNewRgn();
  5738.     hiliteRgn = ODNewRgn();
  5739.  
  5740.     theRect = (**(_fDocument->theTE)).viewRect;
  5741. #if ODDebug && ODDebugCursor
  5742.     PRINT("AdjustCursor Local L: %d T: %d B: %d R: %d\n",theRect.left,theRect.top,theRect.bottom,theRect.right);
  5743. #endif
  5744.     LocalToGlobal((Point *)&(theRect.top));
  5745.     LocalToGlobal((Point *)&(theRect.bottom));
  5746.     RectRgn(iBeamRgn, &theRect);
  5747.  
  5748.     CopyRgn(_fDocument->hiliteRgn, hiliteRgn);
  5749.     SetPt(&thePoint, 0, 0);
  5750.     LocalToGlobal(&thePoint);
  5751.     OffsetRgn(hiliteRgn, thePoint.h, thePoint.v);
  5752.  
  5753.     DiffRgn(arrowRgn, hiliteRgn, arrowRgn);
  5754.     DiffRgn(arrowRgn, iBeamRgn, arrowRgn);
  5755.     DiffRgn(iBeamRgn, hiliteRgn, iBeamRgn);
  5756.  
  5757. #if ODDebug && ODDebugCursor
  5758.     PRINT("AdjustCursor Global H: %d V: %d\n",theLoc.h,theLoc.v);
  5759.     PRINT("AdjustCursor Global L: %d T: %d B: %d R: %d\n",theRect.left,theRect.top,theRect.bottom,theRect.right);
  5760. #endif
  5761.  
  5762.     if (PtInRgn(theLoc, iBeamRgn)) 
  5763.     {
  5764.         SetCursor(*GetCursor(iBeamCursor));
  5765.     } 
  5766.     else if (PtInRgn(theLoc, hiliteRgn)) 
  5767.     {
  5768.         SetCursor(&(ODQDGlobals.arrow));
  5769.     } 
  5770.     else 
  5771.     {
  5772.         SetCursor(&(ODQDGlobals.arrow));
  5773.     }
  5774.     
  5775.     ODDisposeHandle((Handle)iBeamRgn);
  5776.     ODDisposeHandle((Handle)hiliteRgn);
  5777.     ODDisposeHandle((Handle)arrowRgn);
  5778.     SetPort(savePort);
  5779.  
  5780.     SOM_CATCH_ALL
  5781.     SOM_ENDTRY
  5782. }
  5783.  
  5784. SOM_Scope short  SOMLINK AppleTestDrag_DragTextDoDragText(AppleTestDrag_DragText *somSelf, Environment *ev,
  5785.         ODEventData* event)
  5786. {
  5787.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  5788.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","DoDragText");
  5789.  
  5790.     RgnHandle        dragRgn, tempRgn;
  5791.     ODDragAndDrop *dad;
  5792.     ODStorageUnit *dadsu;
  5793.     ODPart        *destPart = kODNULL; ODVolatile(destPart);
  5794.     Point           xForm;
  5795.     
  5796.     SOM_TRY
  5797.  
  5798.     /*
  5799.      *    Wait in the following loop until the user moves the mouse by at
  5800.      *    least 4 pixels. If the mouse is released before moving beyond this
  5801.      *    minimum threshold, return false.
  5802.      */
  5803.     if (!WaitMouseMoved(event->where)) {
  5804.         return(false);
  5805.     }
  5806.  
  5807.     /*
  5808.      *    Set up the drag region for the drag outline
  5809.      */    
  5810.     dragRgn = ODNewRgn();
  5811.     CopyRgn(_fDocument->hiliteRgn, dragRgn);
  5812.     SetPt(&xForm, 0, 0);
  5813.     LocalToGlobal(&xForm);
  5814.     OffsetRgn(dragRgn, xForm.h, xForm.v);
  5815.     tempRgn = ODNewRgn();
  5816.     CopyRgn(dragRgn, tempRgn);
  5817.     InsetRgn(tempRgn, 1, 1);
  5818.     DiffRgn(dragRgn, tempRgn, dragRgn);
  5819.     ODDisposeHandle((Handle)tempRgn);
  5820.  
  5821.     /*
  5822.      *    Set up the drag and drop storage unit
  5823.      */
  5824.     dad = _fSession->GetDragAndDrop(ev);
  5825.     dad->Clear(ev);
  5826.     dadsu = dad->GetContentStorageUnit(ev);
  5827.  
  5828.     if (!HAS_WRITE_ACCESS(_fDraft->GetPermissions(ev)))
  5829.         somSelf->ExternalizeSelection(ev, dadsu, kODCloneCopy);
  5830.     else
  5831.         somSelf->ExternalizeSelection(ev, dadsu, kODCloneCut);
  5832.  
  5833. #ifdef TRYOUT_PROMISE_FILE
  5834.     {
  5835.         PromiseHFSFlavor    thePromise;
  5836.         thePromise.fileType = 'TEXT';
  5837.         thePromise.fileCreator = 'ttxt';
  5838.         thePromise.fdFlags = 0;
  5839.         dadsu->Focus(ev, kODPropContents, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  5840.         StorageUnitSetPromiseValue(dadsu, ev, kODHFSPromise, 0, sizeof(PromiseHFSFlavor), &thePromise, _fPartWrapper);
  5841.     }
  5842. #endif
  5843.  
  5844.     somSelf->AddLinkSpecToSU(ev, dadsu);
  5845.  
  5846.     // Remember the selection in case its moved
  5847.     ODSShort origStart = (**(_fDocument->theTE)).selStart;
  5848.     ODSShort origEnd = (**(_fDocument->theTE)).selEnd;
  5849.     
  5850.     // Also remember the original length of the entire text
  5851.     ODSShort origTextLength = (**(_fDocument->theTE)).teLength;
  5852.     
  5853.     // And the offsets of the link destination, if any.
  5854.     ODSShort origDestStart = _fDstLink.start;
  5855.     ODSShort origDestEnd = _fDstLink.end;
  5856.     ODBoolean destLinkIsAfter = ((origDestStart != -1) && (origDestStart >= origEnd));
  5857.  
  5858.     /*
  5859.      *    Do the drag
  5860.      */
  5861.      // $$$$$ 7/14/94 last parameter changed from EventRecord* to ODEventData* !!!!!
  5862.     
  5863.     ODByteArray dragRgnBA = CreateByteArrayStruct(&dragRgn, sizeof(RgnHandle));
  5864.     ODByteArray eventBA = CreateByteArrayStruct(&event, sizeof(ODEventData*));
  5865.     ODDropResult dropResult = dad->StartDrag(ev, _fDocument->theFrame, kODDragImageRegionHandle, &dragRgnBA, &destPart, &eventBA);
  5866.     DisposeByteArrayStruct(dragRgnBA);
  5867.     DisposeByteArrayStruct(eventBA);
  5868.  
  5869.     if ( dropResult == kODDropMove )
  5870.         PRINT("DragText: StartDrag returned kODDropMove, destPart = %x\n", destPart);
  5871.     else if ( dropResult == kODDropCopy )
  5872.         PRINT("DragText: StartDrag returned kODDropCopy, destPart = %x\n", destPart);
  5873.     else
  5874.         PRINT("DragText: StartDrag returned dropResult = %d\n", dropResult);
  5875.  
  5876.     if ( dropResult == kODDropMove )
  5877.     {
  5878.         // Delete the original content.  Unfortunately, origStart and origEnd may
  5879.         // no longer identify the original content, because the move may have
  5880.         // deposited text ahead of the original content.  Adjust for this if
  5881.         // the current selection is ahead of of origStart.  Note that origStart
  5882.         // will equal selStart if the drag was to another part.
  5883.  
  5884.         ODSShort selStart = (**(_fDocument->theTE)).selStart;
  5885.         ODSShort selEnd = (**(_fDocument->theTE)).selEnd;
  5886.  
  5887.         ODSShort charsAdded = (**(_fDocument->theTE)).teLength - origTextLength;
  5888.  
  5889.         // Note that if text was moved ahead of the original selection, the
  5890.         // original content will have been moved back by the number of characters
  5891.         // inserted.  If the text was moved behind the original selection, the
  5892.         // current selection is at least (charsAdded+1) behind the end of the original
  5893.         // selection (+1 because it must have been moved after the first character
  5894.         // beyond the original selection).
  5895.  
  5896.         // Adjust 'charsAdded' in case the move affected the destination link in this
  5897.         // part, either by making it larger or smaller.
  5898.         if ( destLinkIsAfter )
  5899.         {
  5900.             ODSShort origDestSize = (origDestEnd - origDestStart);
  5901.             ODSShort curDestSize = (_fDstLink.end - _fDstLink.start);
  5902.             charsAdded -= (curDestSize - origDestSize);
  5903.         }
  5904.  
  5905.         if ( selStart <= (origStart+charsAdded) )
  5906.         {
  5907.             // The text was moved ahead of the original selection, so adjust the 
  5908.             // original selection to account for the characters added.
  5909.             // Because white space may have been added, use the total text size
  5910.             // to determine how may characters have been added.
  5911.             origStart += charsAdded;
  5912.             origEnd += charsAdded;
  5913.         }
  5914.         else
  5915.         {
  5916.             // Text was moved behind the original selection, so adjust the current
  5917.             // selection to account for the text about to be deleted.
  5918.             ODSShort origSize = origEnd - origStart;
  5919.             selStart -= origSize;
  5920.             selEnd -= origSize;
  5921.         }
  5922.         
  5923.         //    If the frame is not active, must activate TE before inserting
  5924.         //    text or the background hilite will not update correctly.
  5925.         if ( !_fDocument->frameIsActive )
  5926.             TEActivate(_fDocument->theTE);
  5927.  
  5928.         // Select the moved text and delete it.
  5929.         TESetSelect(origStart, origEnd, _fDocument->theTE);
  5930.         TEDelete(_fDocument->theTE);
  5931.  
  5932.         // If content was moved to another part in this document, and the content contained
  5933.         // the source of a link, don't clear the link!
  5934.         // $$$$$ For now, destPart is null if the drop occurred outside of our
  5935.         // document, but that may change!
  5936.         if ( (destPart != kODNULL) && (destPart != _fPartWrapper) )
  5937.             somSelf->AbandonSourceLinks(ev, origStart, origEnd);
  5938.  
  5939.         if (_fSrcLink.linkSource != ((ODLinkSource*) kODNULL))
  5940.             PRINT("DragText: Adjusting source link after move\n");    
  5941.         somSelf->AdjustLinks(ev, origStart, origEnd, origStart, _fSession->UniqueUpdateID(ev));
  5942.  
  5943.         if (destPart == _fPartWrapper)
  5944.         {
  5945.             // Move within this part
  5946.             TESetSelect(selStart, selEnd, _fDocument->theTE);
  5947.         }
  5948.         else
  5949.         {
  5950.             // Move out of this part
  5951.             TESetSelect(selStart, selStart, _fDocument->theTE);
  5952.         }
  5953.  
  5954.         //    Undo the TEActivate, if needed.
  5955.         if ( !_fDocument->frameIsActive )
  5956.             TEDeactivate(_fDocument->theTE);
  5957.  
  5958.         // If text was moved to another part, it seems necessary to invalidate our
  5959.         // frame to force it to update.
  5960.         if ( charsAdded == 0 )
  5961.             somSelf->InvalAllFrames(ev);
  5962.     }
  5963.  
  5964.     ODReleaseObject(ev, destPart);
  5965.  
  5966.     ODDisposeHandle((Handle)dragRgn);
  5967.  
  5968.     SOM_CATCH_ALL
  5969.         ODReleaseObject(ev, destPart);
  5970.     SOM_ENDTRY
  5971.     
  5972.     return(true);
  5973. }
  5974.  
  5975. //------------------------------------------------------------------------------
  5976. // DragText: HasExtension
  5977. //------------------------------------------------------------------------------
  5978.  
  5979. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextHasExtension(AppleTestDrag_DragText *somSelf, Environment *ev,
  5980.         ODType extensionName)
  5981. {
  5982.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HasExtension");
  5983.  
  5984.     ODBoolean    result = kODFalse;
  5985.  
  5986. #if DRAGTEXT_SUPPORTS_SCRIPTING
  5987.     SOM_TRY
  5988.         if (!ODISOStrCompare((ODISOStr)extensionName, kODExtSemanticInterface))
  5989.             return kODTrue;
  5990.     SOM_CATCH_ALL
  5991.     SOM_ENDTRY
  5992. #endif
  5993.     return result;
  5994. }
  5995.  
  5996. //------------------------------------------------------------------------------
  5997. // DragText: AcquireExtension
  5998. //------------------------------------------------------------------------------
  5999.  
  6000. SOM_Scope ODExtension*     SOMLINK AppleTestDrag_DragTextAcquireExtension(AppleTestDrag_DragText *somSelf, Environment *ev,
  6001.         ODType extensionName)
  6002. {
  6003.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6004.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AcquireExtension");
  6005.  
  6006.     ODExtension*    result = kODNULL;
  6007.  
  6008. #if DRAGTEXT_SUPPORTS_SCRIPTING
  6009.     SOM_TRY
  6010.         if (!ODISOStrCompare((ODISOStr)extensionName, kODExtSemanticInterface))
  6011.         {
  6012.             _fSemtIntf->Acquire(ev);
  6013.             result = _fSemtIntf;
  6014.         }
  6015.     SOM_CATCH_ALL
  6016.     SOM_ENDTRY
  6017. #endif
  6018.     return result;
  6019. }
  6020.  
  6021.  
  6022. //------------------------------------------------------------------------------
  6023. // DragText: ReleaseExtension
  6024. //------------------------------------------------------------------------------
  6025.  
  6026. SOM_Scope void  SOMLINK AppleTestDrag_DragTextReleaseExtension(AppleTestDrag_DragText *somSelf, Environment *ev,
  6027.         ODExtension* extension)
  6028. {
  6029.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6030.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ReleaseExtension");
  6031.  
  6032. #if DRAGTEXT_SUPPORTS_SCRIPTING
  6033.     if( extension == _fSemtIntf )
  6034.         ODDeleteObject(_fSemtIntf);
  6035. #endif
  6036. }
  6037.  
  6038. //------------------------------------------------------------------------------
  6039. // DragText: GetSemanticInterface
  6040. //------------------------------------------------------------------------------
  6041.  
  6042. //SOM_Scope ODCPlusSemanticInterface*  SOMLINK AppleTestDrag_DragTextGetSemanticInterface(AppleTestDrag_DragText *somSelf, Environment *ev)
  6043. //{
  6044. //    AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6045. //    AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","GetSemanticInterface");
  6046. //
  6047. //    return _fSemtIntf;
  6048. //}
  6049.  
  6050. //------------------------------------------------------------------------------
  6051. // DragText: GetTEH
  6052. //------------------------------------------------------------------------------
  6053.  
  6054. //SOM_Scope TEHandle  SOMLINK GetTEH(AppleTestDrag_DragText *somSelf, Environment *ev)
  6055. //{
  6056. //    AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6057. //    AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","GetTEH");
  6058. //
  6059. //    return _fDocument->theTE;
  6060. //}
  6061.  
  6062. //------------------------------------------------------------------------------
  6063. // DragText: InvalAllFrames()
  6064. //------------------------------------------------------------------------------
  6065.  
  6066. SOM_Scope void  SOMLINK AppleTestDrag_DragTextInvalAllFrames(AppleTestDrag_DragText *somSelf, Environment *ev)
  6067. {
  6068.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6069.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InvalAllFrames");
  6070.  
  6071.     SOM_TRY
  6072.  
  6073.     OrderedCollectionIterator dispIter(_fDisplayFrames);
  6074.     for (ODFrame* aFrame = (ODFrame*)dispIter.First();
  6075.          dispIter.IsNotComplete();
  6076.          aFrame = (ODFrame*)dispIter.Next())
  6077.     {
  6078.         aFrame->Invalidate(ev, (ODShape*)kODNULL, kODNULL) ;
  6079.     }
  6080.  
  6081.     SOM_CATCH_ALL
  6082.     SOM_ENDTRY
  6083. }    // InvalAllFrames()
  6084.  
  6085. //------------------------------------------------------------------------------
  6086. // DragText: GetDragTextFrame()
  6087. //------------------------------------------------------------------------------
  6088.  
  6089. //SOM_Scope ODFrame*  SOMLINK GetDragTextFrame(AppleTestDrag_DragText *somSelf, Environment *ev)
  6090. //{
  6091. //    AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6092. //    AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","GetDragTextFrame");
  6093. //
  6094. //    return _fDocument->theFrame ;
  6095. //}
  6096.  
  6097. //------------------------------------------------------------------------------
  6098. // DragText: AdjustMenus()
  6099. //------------------------------------------------------------------------------
  6100.  
  6101. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAdjustMenus(AppleTestDrag_DragText *somSelf, Environment *ev,
  6102.         ODFrame* frame)
  6103. {
  6104.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6105.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustMenus");
  6106.  
  6107.     SOM_TRY
  6108.  
  6109.     TEHandle    myTE = _fDocument->theTE;
  6110.     ODBoolean    isSelection;
  6111.     ODBoolean    canSelectAll;
  6112.     ODBoolean    canShowLinkInfo;
  6113.  
  6114.     // DON'T DO THIS! FOR LOCALISABILITY, READ THE TEXT OUT OF A RESOURCE AND
  6115.     //    USE THE CURRENT SYSTEM SCRIPT AND CURRENT SYSTEM LANGUAGE.
  6116.     ODIText* menuItem = CreateIText(smRoman, langEnglish, "About DragText…");
  6117.     _fMenuBar->SetItemString(ev, kODCommandAbout, menuItem);
  6118.     DisposeIText(menuItem);
  6119.  
  6120.     isSelection = (*myTE)->selEnd > (*myTE)->selStart;
  6121.     _fMenuBar->EnableCommand(ev, kODCommandClear, isSelection);
  6122.  
  6123.     ODArbitrator* arbitrator = _fSession->GetArbitrator(ev);
  6124.  
  6125.     ODFrame* clipboardFocus = arbitrator->AcquireFocusOwner(ev, _fClipboardFocus);
  6126.     if ( (frame == clipboardFocus) || 
  6127.          (arbitrator->RequestFocus(ev, _fClipboardFocus, frame)) )
  6128.     {
  6129.         ODBoolean canPaste = kODFalse;
  6130. #ifdef SUPPORT_TESTDRAG_KIND
  6131.          canPaste = ValueOnClipboard(ev, kODKindTestDrag, _fSession);
  6132. #endif
  6133.          canPaste |= ValueOnClipboard(ev, kODAppleTEXT, _fSession);
  6134.         _fMenuBar->EnableCommand(ev, kODCommandCut, isSelection);
  6135.         _fMenuBar->EnableCommand(ev, kODCommandCopy, isSelection);
  6136.         _fMenuBar->EnableCommand(ev, kODCommandPaste, canPaste);
  6137.         _fMenuBar->EnableCommand(ev, kODCommandPasteAs, canPaste);
  6138.     }
  6139.     ODReleaseObject(ev, clipboardFocus);
  6140.     
  6141.     canSelectAll = ((*myTE)->teLength > 0) && (((*myTE)->selStart > 0) || ((*myTE)->selEnd < (*myTE)->teLength));
  6142.     _fMenuBar->EnableCommand(ev, kODCommandSelectAll, canSelectAll);
  6143.  
  6144.     canShowLinkInfo = somSelf->SelectionInLinkSource(ev) || somSelf->SelectionInLinkDestination(ev);
  6145.  
  6146.     Str255 aStr;
  6147.     CopyPascalString(aStr, canShowLinkInfo ? "\pLink Info" : "\pPart Info");
  6148.     ODScriptCode script = FontToScript( GetSysFont() );
  6149.     ODLangCode lang = GetScriptVariable(script, smScriptLang);
  6150.     ODIText* menuText = CreateIText(script, lang, aStr);
  6151.     _fMenuBar->SetItemString(ev, kODCommandGetPartInfo, menuText);
  6152.     DisposeIText(menuText);
  6153.     _fMenuBar->EnableCommand(ev, kODCommandGetPartInfo, canShowLinkInfo);
  6154.  
  6155.     somSelf->AdjustFontMenu(ev);
  6156.     somSelf->AdjustSizeMenu(ev);
  6157.  
  6158.     SOM_CATCH_ALL
  6159.     SOM_ENDTRY
  6160. }
  6161.  
  6162. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCommonInitDragText(AppleTestDrag_DragText *somSelf, Environment *ev,
  6163.         ODStorageUnit* su)
  6164. {
  6165.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6166.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CommonInitDragText");
  6167.  
  6168.     SOM_TRY
  6169.  
  6170.     _fSession = su->GetSession(ev);
  6171.  
  6172.     AppleTestDrag_SemtIntf* face = new AppleTestDrag_SemtIntf;
  6173.     face->InitSemtIntf(ev, somSelf, _fSession);
  6174.     _fSemtIntf = face;
  6175.  
  6176.     _fFocusSet = _fSession->GetArbitrator(ev)->CreateFocusSet(ev);
  6177.     _fFocusSet->Add(ev, _fSession->Tokenize(ev, kODKeyFocus));
  6178.     _fFocusSet->Add(ev, _fSession->Tokenize(ev, kODMenuFocus));
  6179.     _fFocusSet->Add(ev, _fSession->Tokenize(ev, kODSelectionFocus));
  6180.  
  6181.     _fClipboardFocus = _fSession->Tokenize(ev, kODClipboardFocus);
  6182.     
  6183.     _fDisplayFrames = new OrderedCollection;
  6184.  
  6185. #ifdef SHOW_TEXT_MENU
  6186.     _fTextMenu = NewMenu(140,"\pText");
  6187.     if (!_fFontMenu)
  6188.         WARN("Error creating Text menu");
  6189. #else
  6190.     _fTextMenu = kODNULL;
  6191. #endif
  6192.  
  6193.     _fFontMenu = NewMenu(141,"\pFont");
  6194.     if (!_fFontMenu)
  6195.         WARN("Error creating Font menu");
  6196.  
  6197.     _fSizeMenu = NewMenu(142,"\pSize");
  6198.     if (!_fSizeMenu)
  6199.         WARN("Error creating Size menu");
  6200.     
  6201.     _fMenuBar = _fSession->GetWindowState(ev)->CopyBaseMenuBar(ev);
  6202.  
  6203. #ifdef SHOW_TEXT_MENU
  6204.     AppendMenu(_fTextMenu,"\pSample One");
  6205.     _fMenuBar->RegisterCommand(ev, cDTSampleOne, 140, 1);
  6206.     AppendMenu(_fTextMenu,"\pSample Two");
  6207.     _fMenuBar->RegisterCommand(ev, cDTSampleTwo, 140, 2);
  6208. #endif
  6209.     
  6210.     AppendResMenu(_fFontMenu,'FOND');
  6211.     
  6212.     AppendMenu(_fSizeMenu,"\p9");
  6213.     AppendMenu(_fSizeMenu,"\p10");
  6214.     AppendMenu(_fSizeMenu,"\p11");
  6215.     AppendMenu(_fSizeMenu,"\p12");
  6216.     AppendMenu(_fSizeMenu,"\p14");
  6217.     AppendMenu(_fSizeMenu,"\p18");
  6218.     AppendMenu(_fSizeMenu,"\p24");
  6219.     AppendMenu(_fSizeMenu,"\p36");
  6220.  
  6221. #ifdef SHOW_TEXT_MENU
  6222.     _fMenuBar->AddMenuLast(ev, 140, _fTextMenu, _fPartWrapper);
  6223. #endif
  6224.     _fMenuBar->AddMenuLast(ev, 141, _fFontMenu, _fPartWrapper);
  6225.     _fMenuBar->AddMenuLast(ev, 142, _fSizeMenu, _fPartWrapper);
  6226.  
  6227. #ifdef SIHELPER_IMPLEMENTED
  6228. // $$$$$
  6229. //    _fSemtIntf = new ODCPlusSemanticInterface();
  6230. //    _fSemtIntf->InitCPlusSemanticInterface(ev, _fPartWrapper);
  6231. #endif
  6232. //    somSelf->InstallObjectAccessors(ev);
  6233.  
  6234.     SOM_CATCH_ALL
  6235.     SOM_ENDTRY
  6236. }
  6237.  
  6238. SOM_Scope void  SOMLINK AppleTestDrag_DragTextExternalizeSelection(AppleTestDrag_DragText *somSelf, Environment *ev,
  6239.         ODStorageUnit* su,
  6240.         ODCloneKind cloneKind)
  6241. {
  6242.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6243.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ExternalizeSelection");
  6244.  
  6245.     SOM_TRY
  6246.  
  6247.     // Write out the current selection to the argument storage unit
  6248.     // This routine can be called to write to an existing storage unit; however,
  6249.     // only the values written will be changed.
  6250.  
  6251. #ifdef SUPPORT_TESTDRAG_KIND
  6252.     ODStorageUnitRef    aSURef;
  6253. #endif
  6254.  
  6255.     ODSShort    selStart    = (**(_fDocument->theTE)).selStart;
  6256.     ODSShort    selEnd        = (**(_fDocument->theTE)).selEnd;
  6257.  
  6258.     ODBoolean    srcLinkIn    = kODFalse;
  6259.     ODBoolean    dstLinkIn    = kODFalse;
  6260.     ODULong        linkCount    = 0;
  6261.     
  6262.     if ( !(cloneKind == kODCloneToLink) )
  6263.     {
  6264.         if ( _fSrcLink.linkSource != (ODLinkSource*) kODNULL )
  6265.             srcLinkIn = IncludedInRange(_fSrcLink.start, _fSrcLink.end, selStart, selEnd);
  6266.     
  6267.         if ( _fDstLink.link != (ODLink*) kODNULL )
  6268.             dstLinkIn = IncludedInRange(_fDstLink.start, _fDstLink.end, selStart, selEnd);
  6269.     
  6270.         if ( srcLinkIn )
  6271.             linkCount += 1;
  6272.         if ( dstLinkIn )
  6273.             linkCount += 1;
  6274.     }
  6275.  
  6276.     ODDraft* toDraft = su->GetDraft(ev);
  6277.  
  6278.     ODDraftKey draftKey = _fDraft->BeginClone(ev, toDraft, kODNULL, cloneKind);
  6279.     TRY
  6280.  
  6281. #ifdef SUPPORT_TESTDRAG_KIND
  6282.         ODStorageUnit*    testDragSU;
  6283. #endif
  6284.         // Add the contents property to argument storage unit
  6285.         SUForceFocus(ev, su, kODPropContents, kODNULL);
  6286.     
  6287. #ifdef SUPPORT_TESTDRAG_KIND
  6288.         // If a test drag storage unit already exists, re-use it rather than
  6289.         // create a new one so this routine can be used to update a link.
  6290.         SUForceFocus(ev, su, kODPropContents, kODKindTestDrag);
  6291.         if ( su->GetSize(ev) == sizeof(ODStorageUnitRef) )
  6292.         {
  6293.             StorageUnitGetValue(su, ev, sizeof(ODStorageUnitRef), &aSURef);
  6294.             testDragSU = toDraft->AcquireStorageUnit(ev, su->GetIDFromStorageUnitRef(ev, aSURef));
  6295.         }
  6296.         else
  6297.         {
  6298.             testDragSU = toDraft->CreateStorageUnit(ev);
  6299.     
  6300.             // Add the TestDrag content value (a reference to another storage unit)
  6301.             su->GetStrongStorageUnitRef(ev, testDragSU->GetID(ev), aSURef);
  6302.             StorageUnitSetValue(su, ev, sizeof(ODStorageUnitRef), aSURef);
  6303.         }
  6304. #endif
  6305.     
  6306.         WriteValueType(ev, su, kODPropContents, kODApplestxt, _fDocument->theTE, kODTrue);
  6307.         WriteValueType(ev, su, kODPropContents, kODAppleTEXT, _fDocument->theTE, kODTrue);
  6308.  
  6309. #ifdef SUPPORT_TESTDRAG_KIND
  6310.         // …and to the TestDrag storage unit
  6311.         WriteValueType(ev, testDragSU, kDragTextPropText, kODAppleTEXT, _fDocument->theTE, kODTrue);
  6312.         WriteValueType(ev, testDragSU, kDragTextPropStyl, kODApplestyl, _fDocument->theTE, kODTrue);
  6313. #endif
  6314.  
  6315. #ifdef SUPPORT_TESTDRAG_KIND
  6316.         if ( linkCount )
  6317.         {
  6318.             // Write out the source and destination links
  6319.             if ( srcLinkIn )
  6320.             {
  6321.                 ODID clonedLinkSourceID = _fDraft->Clone(ev, draftKey, _fSrcLink.linkSource->GetID(ev), 0, 0);
  6322.  
  6323.                 SUForceFocus(ev, testDragSU, kDragTextPropSourceLink, kDragTextValue);
  6324.                 testDragSU->GetStrongStorageUnitRef(ev, clonedLinkSourceID, aSURef);
  6325.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODStorageUnitRef),aSURef);
  6326.                 ODSShort srcLinkStart = _fSrcLink.start - selStart;
  6327.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &srcLinkStart);
  6328.                 ODSShort srcLinkEnd = _fSrcLink.end - selStart;
  6329.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &srcLinkEnd);
  6330.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODULong), (ODValue) &_fSrcLink.change);
  6331.             }
  6332.         
  6333.             if ( dstLinkIn )
  6334.             {
  6335.                 ODID clonedLinkID = _fDraft->Clone(ev, draftKey, _fDstLink.link->GetID(ev), 0, 0);
  6336.  
  6337.                 SUForceFocus(ev, testDragSU, kDragTextPropDestLink, kDragTextValue);
  6338.                 testDragSU->GetStrongStorageUnitRef(ev, clonedLinkID, aSURef);
  6339.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODStorageUnitRef), aSURef);
  6340.                 ODSShort dstLinkStart = _fDstLink.start - selStart;
  6341.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &dstLinkStart);
  6342.                 ODSShort dstLinkEnd = _fDstLink.end - selStart;
  6343.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &dstLinkEnd);
  6344.                 
  6345.                 ODType kind = _fDstLink.info.kind;
  6346.                 _fDstLink.info.kind = (ODType) ODISOStrLength(kind) + 1;
  6347.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODLinkInfo), (ODValue) &_fDstLink.info);
  6348.                 StorageUnitSetValue(testDragSU, ev, (ODULong) _fDstLink.info.kind, (ODValue) kind);
  6349.                 _fDstLink.info.kind = kind;
  6350.             }
  6351.         }
  6352.  
  6353.     testDragSU->Release(ev);
  6354.  
  6355. #endif
  6356.  
  6357.     CATCH_ALL
  6358.         _fDraft->AbortClone(ev, draftKey);
  6359.         RERAISE;
  6360.     ENDTRY
  6361.     _fDraft->EndClone(ev, draftKey);
  6362.  
  6363.     SOM_CATCH_ALL
  6364.     SOM_ENDTRY
  6365. }
  6366.  
  6367. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextCanPasteLink(AppleTestDrag_DragText *somSelf, Environment *ev,
  6368.                             ODStorageUnit*    contentSU,
  6369.                             ODBoolean        isMyContent,
  6370.                             ODSShort        pasteStart,
  6371.                             ODSShort        pasteEnd)
  6372. {
  6373.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6374.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CanPasteLink");
  6375.  
  6376.     ODBoolean result = kODFalse;
  6377.  
  6378. #ifdef SUPPORT_TESTDRAG_KIND
  6379.     if (HAS_WRITE_ACCESS(_fDraft->GetPermissions(ev)))
  6380.     {
  6381.         result = (_fDstLink.link == (ODLink*) kODNULL) && LinkSpecInSU(ev, contentSU);
  6382.         if ( result )
  6383.         {
  6384.             // A link spec is present, and this part can support a link destination
  6385.             if ( _fSrcLink.linkSource != kODNULL )
  6386.             {
  6387.                 // Do not allow the destination to overlap an existing link source
  6388.                 result = !RangesOverlap(pasteStart,pasteEnd,_fSrcLink.start,_fSrcLink.end);
  6389.             }
  6390.             else if ( isMyContent )
  6391.             {
  6392.                 // The link spec is to a potential link source in this part.
  6393.                 // Do not allow the destination to overlap the potential link source
  6394.                 if (_fSrcLink.end > _fSrcLink.start)    // Must always be true!
  6395.                     result = !RangesOverlap(pasteStart,pasteEnd,_fSrcLink.start,_fSrcLink.end);
  6396.             }
  6397.         }
  6398.     }
  6399. #endif
  6400.  
  6401.     return result;
  6402. }
  6403.  
  6404. SOM_Scope void  SOMLINK AppleTestDrag_DragTextInstallObjectAccessors(AppleTestDrag_DragText *somSelf, Environment *ev)
  6405. {
  6406.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6407.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InstallObjectAccessors");
  6408.  
  6409. }
  6410.  
  6411. SOM_Scope void  SOMLINK AppleTestDrag_DragTextsomInit(AppleTestDrag_DragText *somSelf)
  6412. {
  6413.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6414.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","somInit");
  6415.  
  6416.     AppleTestDrag_DragText_parents_somInit(somSelf);
  6417.  
  6418.     _fHasFoci = kODFalse;
  6419.     _fNeedsFoci = kODFalse;
  6420.     _fFocusSet = kODNULL;
  6421.     _fClipboardFocus = kODNullFocus;
  6422.     _fDirty = kODFalse;    
  6423.  
  6424.     _fWindowID = 0;
  6425.     _fDocument = NULL;
  6426.     _fTextHandle = NULL;
  6427.     _fStylHandle = NULL;
  6428.     _fExtTransStore.x = 0;        // ••• hack! so we can tell if frame has been dragged
  6429.     _fExtTransStore.y = 0;
  6430.     _fLastFont = -1;
  6431.     _fLastSize = 0;
  6432.     _fFrameShapeChanged = true;
  6433.     _fDisplayFrames = kODNULL;
  6434.     _fUndoCount = 0;
  6435.     _fRedoCount = 0;
  6436.  
  6437.     _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  6438.     _fSrcLink.start = -1;
  6439.     _fSrcLink.end = -1;
  6440.     _fSrcLink.change = kODUnknownUpdate;
  6441.  
  6442.     _fDstLink.link = (ODLink*) kODNULL;
  6443.     _fDstLink.start = -1;
  6444.     _fDstLink.end = -1;
  6445.     _fDstLink.info.kind = (ODType) kODNULL;
  6446.  
  6447.     _fLinkBorderStyle = kLinkBorderOff;
  6448.     _fTimeBorderOff = 0;
  6449.  
  6450.     _fDraft = kODNULL;
  6451.     _fTestDragSU = kODNULL;
  6452.  
  6453. //    _fSemtIntf = kODNULL;
  6454.     _fPartWrapper = kODNULL;
  6455. }
  6456.  
  6457. SOM_Scope void  SOMLINK AppleTestDrag_DragTextsomUninit(AppleTestDrag_DragText *somSelf)
  6458. {
  6459.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6460.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","somUninit");
  6461.  
  6462.     Environment* ev = somGetGlobalEnvironment();
  6463.  
  6464.     // DragText::ReleaseExtension will get called as a result of the refcount of
  6465.     //    _fSemtIntf going to zero. ReleaseExtension will then delete the object.
  6466.     _fSemtIntf->Release(ev);
  6467.  
  6468.     ODDeleteObject(_fDisplayFrames);
  6469.  
  6470.     if ( _fDocument && _fDocument->theTE )
  6471.         TEDispose(_fDocument->theTE);
  6472.     ODDisposePtr((Ptr) _fDocument);
  6473.  
  6474.     ODDisposeHandle((Handle) _fTextHandle);
  6475.     ODDisposeHandle((Handle) _fStylHandle);
  6476.  
  6477.     if ( _fMenuBar )
  6478.     {    
  6479. #ifdef SHOW_TEXT_MENU
  6480.         _fMenuBar->RemoveMenu(ev, 140);
  6481. #endif
  6482.         _fMenuBar->RemoveMenu(ev, 141);
  6483.         _fMenuBar->RemoveMenu(ev, 142);
  6484.         _fMenuBar->Release(ev);
  6485.     }
  6486.  
  6487.     if (_fTextMenu) DisposeMenu(_fTextMenu);
  6488.     if (_fFontMenu) DisposeMenu(_fFontMenu);
  6489.     if (_fSizeMenu) DisposeMenu(_fSizeMenu);
  6490.  
  6491.     ODDeleteObject(_fFocusSet);
  6492.  
  6493.     AppleTestDrag_DragText_parents_somUninit(somSelf);
  6494. }
  6495.  
  6496. SOM_Scope void  SOMLINK AppleTestDrag_DragTextContainingPartPropertiesUpdated(AppleTestDrag_DragText *somSelf, Environment *ev,
  6497.         ODFrame* frame,
  6498.         ODStorageUnit* propertyUnit)
  6499. {
  6500.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6501.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ContainingPartPropertiesUpdated");
  6502.  
  6503.     ODUnused(frame);
  6504.     ODUnused(propertyUnit);
  6505. }
  6506.  
  6507. SOM_Scope ODStorageUnit*  SOMLINK AppleTestDrag_DragTextAcquireContainingPartProperties(AppleTestDrag_DragText *somSelf, Environment *ev,
  6508.         ODFrame* frame)
  6509. {
  6510.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6511.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AcquireContainingPartProperties");
  6512.  
  6513.     ODUnused(frame);
  6514.     
  6515.     ODSetSOMException(ev, kODErrCannotEmbed);
  6516.  
  6517.     return kODNULL;
  6518. }
  6519.  
  6520. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextRevealFrame(AppleTestDrag_DragText *somSelf, Environment *ev,
  6521.         ODFrame* embeddedFrame,
  6522.         ODShape* revealShape)
  6523. {
  6524.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6525.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RevealFrame");
  6526.  
  6527.     ODUnused(embeddedFrame);
  6528.     ODUnused(revealShape);
  6529.  
  6530.     ODSetSOMException(ev, kODErrInvalidFrame);
  6531.     return kODFalse;
  6532. }
  6533.  
  6534. SOM_Scope void  SOMLINK AppleTestDrag_DragTextEmbeddedFrameSpec(AppleTestDrag_DragText *somSelf, Environment *ev,
  6535.         ODFrame* embeddedFrame,
  6536.         ODObjectSpec* spec)
  6537. {
  6538.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6539.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","EmbeddedFrameSpec");
  6540.  
  6541.     ODUnused(embeddedFrame);
  6542.     ODUnused(spec);
  6543.  
  6544.     ODSetSOMException(ev, kODErrInvalidFrame);
  6545. }
  6546.  
  6547. SOM_Scope ODEmbeddedFramesIterator*  SOMLINK AppleTestDrag_DragTextCreateEmbeddedFramesIterator(AppleTestDrag_DragText *somSelf, Environment *ev,
  6548.         ODFrame* frame)
  6549. {
  6550.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6551.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CreateEmbeddedFramesIterator");
  6552.  
  6553.     ODUnused(frame);
  6554.  
  6555.     ODSetSOMException(ev, kODErrCannotEmbed);
  6556.  
  6557.     return kODNULL;
  6558. }
  6559.  
  6560. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAttachSourceFrame(AppleTestDrag_DragText *somSelf, Environment *ev,
  6561.         ODFrame* frame,
  6562.         ODFrame* sourceFrame)
  6563. {
  6564.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6565.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AttachSourceFrame");
  6566.  
  6567.     ODUnused(frame);
  6568.     ODUnused(sourceFrame);
  6569. }
  6570.  
  6571. SOM_Scope void  SOMLINK AppleTestDrag_DragTextViewTypeChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6572.         ODFrame* frame)
  6573. {
  6574.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6575.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ViewTypeChanged");
  6576.  
  6577.     ODUnused(frame);
  6578. }
  6579.  
  6580. SOM_Scope void  SOMLINK AppleTestDrag_DragTextPresentationChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6581.         ODFrame* frame)
  6582. {
  6583.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6584.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","PresentationChanged");
  6585.  
  6586.     ODUnused(frame);
  6587. }
  6588.  
  6589. SOM_Scope void  SOMLINK AppleTestDrag_DragTextSequenceChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6590.         ODFrame* frame)
  6591. {
  6592.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6593.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","SequenceChanged");
  6594.  
  6595.     ODUnused(frame);
  6596. }
  6597.  
  6598. SOM_Scope ODInfoType  SOMLINK AppleTestDrag_DragTextReadPartInfo(AppleTestDrag_DragText *somSelf, Environment *ev,
  6599.         ODFrame* frame,
  6600.         ODStorageUnitView* storageUnitView)
  6601. {
  6602.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6603.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ReadPartInfo");
  6604.  
  6605.     ODUnused(frame);
  6606.     ODUnused(storageUnitView);
  6607.  
  6608.     return kODNULL;
  6609. }
  6610.  
  6611. SOM_Scope void  SOMLINK AppleTestDrag_DragTextWritePartInfo(AppleTestDrag_DragText *somSelf, Environment *ev,
  6612.         ODInfoType partInfo,
  6613.         ODStorageUnitView* storageUnitView)
  6614. {
  6615.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6616.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","WritePartInfo");
  6617.  
  6618.     ODUnused(partInfo);
  6619.     ODUnused(storageUnitView);
  6620. }
  6621.  
  6622. SOM_Scope void  SOMLINK AppleTestDrag_DragTextClonePartInfo(AppleTestDrag_DragText *somSelf, Environment *ev,
  6623.         ODDraftKey key,
  6624.         ODInfoType partInfo,
  6625.         ODStorageUnitView* storageUnitView,
  6626.         ODFrame* scopeFrame)
  6627. {
  6628.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6629.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ClonePartInfo");
  6630.  
  6631.     ODUnused(key);
  6632.     ODUnused(partInfo);
  6633.     ODUnused(storageUnitView);
  6634.     ODUnused(scopeFrame);
  6635. }
  6636.  
  6637. SOM_Scope ODFrame*  SOMLINK AppleTestDrag_DragTextRequestEmbeddedFrame(AppleTestDrag_DragText *somSelf, Environment *ev,
  6638.         ODFrame* containingFrame,
  6639.         ODFrame* baseFrame,
  6640.         ODShape* frameShape,
  6641.         ODPart* embedPart,
  6642.         ODTypeToken viewType,
  6643.         ODTypeToken presentation,
  6644.         ODBoolean isOverlaid)
  6645. {
  6646.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6647.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RequestEmbeddedFrame");
  6648.  
  6649.     ODUnused(containingFrame);
  6650.     ODUnused(baseFrame);
  6651.     ODUnused(frameShape);
  6652.     ODUnused(embedPart);
  6653.     ODUnused(viewType);
  6654.     ODUnused(presentation);
  6655.     ODUnused(isOverlaid);
  6656.  
  6657.     ODSetSOMException(ev, kODErrCannotEmbed);
  6658.  
  6659.     return kODNULL;
  6660. }
  6661.  
  6662. SOM_Scope void  SOMLINK AppleTestDrag_DragTextRemoveEmbeddedFrame(AppleTestDrag_DragText *somSelf, Environment *ev,
  6663.         ODFrame* embeddedFrame)
  6664. {
  6665.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6666.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RemoveEmbeddedFrame");
  6667.  
  6668.     ODUnused(embeddedFrame);
  6669. }
  6670.  
  6671. SOM_Scope ODShape*  SOMLINK AppleTestDrag_DragTextRequestFrameShape(AppleTestDrag_DragText *somSelf, Environment *ev,
  6672.         ODFrame* embeddedFrame,
  6673.         ODShape* frameShape)
  6674. {
  6675.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6676.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","RequestFrameShape");
  6677.  
  6678.     ODUnused(embeddedFrame);
  6679.  
  6680.     return (frameShape);
  6681. }
  6682.  
  6683. SOM_Scope void  SOMLINK AppleTestDrag_DragTextUsedShapeChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6684.         ODFrame* embeddedFrame)
  6685. {
  6686.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6687.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","UsedShapeChanged");
  6688.  
  6689.     ODUnused(embeddedFrame);
  6690. }
  6691.  
  6692. SOM_Scope ODShape*  SOMLINK AppleTestDrag_DragTextAdjustBorderShape(AppleTestDrag_DragText *somSelf, Environment *ev,
  6693.         ODFacet* embeddedFacet,
  6694.         ODShape* shape)
  6695. {
  6696.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6697.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AdjustBorderShape");
  6698.  
  6699.     ODUnused(embeddedFacet);
  6700.  
  6701.     return shape;
  6702. }
  6703.  
  6704. SOM_Scope void  SOMLINK AppleTestDrag_DragTextFacetAdded(AppleTestDrag_DragText *somSelf, Environment *ev,
  6705.         ODFacet* facet)
  6706. {
  6707.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6708.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","FacetAdded");
  6709.  
  6710.     SOM_TRY
  6711.  
  6712.     somSelf->CreateDocument(ev, facet->GetFrame(ev));
  6713.  
  6714. //    PRINT("DragText %d: FacetAdded: Registering for Idle events\n", somSelf->GetID(ev));
  6715.  
  6716.     _fSession->GetDispatcher(ev)->RegisterIdle(ev, _fPartWrapper, facet->GetFrame(ev), kDTIdleFrequency);
  6717.  
  6718.     SOM_CATCH_ALL
  6719.     SOM_ENDTRY
  6720. }
  6721.  
  6722. SOM_Scope void  SOMLINK AppleTestDrag_DragTextFacetRemoved(AppleTestDrag_DragText *somSelf, Environment *ev,
  6723.         ODFacet* facet)
  6724. {
  6725.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6726.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","FacetRemoved");
  6727.  
  6728.     ODUnused(facet);
  6729.  
  6730.     SOM_TRY
  6731.  
  6732. //    PRINT("DragText %d: FacetRemoved: Unregistering for Idle events\n", somSelf->GetID(ev));
  6733.  
  6734.     _fSession->GetDispatcher(ev)->UnregisterIdle(ev, _fPartWrapper, facet->GetFrame(ev));
  6735.  
  6736.     SOM_CATCH_ALL
  6737.     SOM_ENDTRY
  6738. }
  6739.  
  6740. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCanvasChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6741.         ODFacet* facet)
  6742. {
  6743.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6744.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CanvasChanged");
  6745.  
  6746.     ODUnused(facet);
  6747. }
  6748.  
  6749. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCanvasUpdated(AppleTestDrag_DragText *somSelf, Environment *ev,
  6750.         ODCanvas* canvas)
  6751. {
  6752.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6753.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CanvasUpdated");
  6754.  
  6755.     ODUnused(canvas);
  6756. }
  6757.  
  6758. SOM_Scope void  SOMLINK AppleTestDrag_DragTextHighlightChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6759.         ODFacet* facet)
  6760. {
  6761.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6762.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","HighlightChanged");
  6763.  
  6764.     ODUnused(facet);
  6765. }
  6766.  
  6767. SOM_Scope ODULong  SOMLINK AppleTestDrag_DragTextGetPrintResolution(AppleTestDrag_DragText *somSelf, Environment *ev,
  6768.         ODFrame* frame)
  6769. {
  6770.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6771.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","GetPrintResolution");
  6772.  
  6773.     ODUnused(frame);
  6774.  
  6775.     return (0);
  6776. }
  6777.  
  6778. SOM_Scope void  SOMLINK AppleTestDrag_DragTextEmbeddedFrameUpdated(AppleTestDrag_DragText *somSelf, Environment *ev,
  6779.         ODFrame* frame,
  6780.         ODUpdateID change)
  6781. {
  6782.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6783.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","EmbeddedFrameUpdated");
  6784.  
  6785.     ODUnused(frame);
  6786.     ODUnused(change);
  6787. }
  6788.  
  6789. SOM_Scope void  SOMLINK AppleTestDrag_DragTextLinkStatusChanged(AppleTestDrag_DragText *somSelf, Environment *ev,
  6790.         ODFrame* frame)
  6791. {
  6792.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6793.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","LinkStatusChanged");
  6794.  
  6795.     ODUnused(frame);
  6796. }
  6797.  
  6798. SOM_Scope ODBoolean  SOMLINK AppleTestDrag_DragTextBeginRelinquishFocus(AppleTestDrag_DragText *somSelf, Environment *ev,
  6799.         ODTypeToken focus,
  6800.         ODFrame* ownerFrame,
  6801.         ODFrame* proposedFrame)
  6802. {
  6803.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6804.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","BeginRelinquishFocus");
  6805.  
  6806.     ODUnused(focus);
  6807.     ODUnused(ownerFrame);
  6808.     ODUnused(proposedFrame);
  6809.  
  6810.     return kODTrue;
  6811. }
  6812.  
  6813. SOM_Scope void  SOMLINK AppleTestDrag_DragTextAbortRelinquishFocus(AppleTestDrag_DragText *somSelf, Environment *ev,
  6814.         ODTypeToken focus,
  6815.         ODFrame* ownerFrame,
  6816.         ODFrame* proposedFrame)
  6817. {
  6818.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6819.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","AbortRelinquishFocus");
  6820.  
  6821.     ODUnused(focus);
  6822.     ODUnused(ownerFrame);
  6823.     ODUnused(proposedFrame);
  6824. }
  6825.  
  6826. SOM_Scope void  SOMLINK AppleTestDrag_DragTextFocusAcquired(AppleTestDrag_DragText *somSelf, Environment *ev,
  6827.         ODTypeToken focus,
  6828.         ODFrame* ownerFrame)
  6829. {
  6830.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6831.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","FocusAcquired");
  6832.  
  6833.     ODUnused(focus);
  6834.     ODUnused(ownerFrame);
  6835. }
  6836.  
  6837. SOM_Scope void  SOMLINK AppleTestDrag_DragTextFocusLost(AppleTestDrag_DragText *somSelf, Environment *ev,
  6838.         ODTypeToken focus,
  6839.         ODFrame* ownerFrame)
  6840. {
  6841.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6842.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","FocusLost");
  6843.  
  6844.     ODUnused(focus);
  6845.     ODUnused(ownerFrame);
  6846.     if (focus == _fSession->Tokenize(ev, kODMenuFocus)) 
  6847.     {
  6848.         somSelf->RemoveMenus(ev, ownerFrame);
  6849.         _fHasFoci = kODFalse;
  6850.     }
  6851. }
  6852.  
  6853. //-------------------------------------------------------------------------
  6854. // DragText: CloneInto (Override)
  6855. //-------------------------------------------------------------------------
  6856.  
  6857. SOM_Scope void  SOMLINK AppleTestDrag_DragTextCloneInto(AppleTestDrag_DragText *somSelf, Environment *ev,
  6858.         ODDraftKey key,
  6859.         ODStorageUnit* storageUnit,
  6860.         ODFrame* scopeFrame)
  6861. {
  6862.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6863.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","CloneInto");
  6864.  
  6865.     // If one of our properties already exists, this object has been cloned already
  6866.     if ( storageUnit->Exists(ev, kODPropContents, kODNULL, 0) )
  6867.         return;
  6868.  
  6869.     SOM_TRY
  6870.  
  6871.     parent_CloneInto(somSelf, ev, key, storageUnit, scopeFrame);
  6872.  
  6873.     if ( _fDocument != kODNULL )
  6874.     {
  6875.         StScrpHandle        stylHandle;
  6876.         Handle                textHandle;
  6877.         char                textState;
  6878.         ODULong                textSize;
  6879.         void*                textPtr;
  6880.         ODStorageUnitRef    aSURef;
  6881.  
  6882.         ODID scopeFrameID = ( scopeFrame == kODNULL ? 0 : scopeFrame->GetID(ev) );
  6883.         ODID clonedID;
  6884.  
  6885. #ifdef SUPPORT_TESTDRAG_KIND
  6886.         ODStorageUnit* testDragSU = storageUnit->GetDraft(ev)->CreateStorageUnit(ev);
  6887. #endif
  6888.     
  6889.         textHandle = (**(_fDocument->theTE)).hText;
  6890.         textSize = (**(_fDocument->theTE)).teLength;
  6891.         textState = HGetState(textHandle);
  6892.         textPtr = ODLockHandle((ODHandle) textHandle);
  6893.  
  6894.         // Write the text to the content property
  6895.         SUFocusAndSetValue(ev, storageUnit, kODPropContents, kODAppleTEXT, (ODValue) textPtr, textSize);
  6896.     
  6897. #ifdef SUPPORT_TESTDRAG_KIND
  6898.         // …and to the TestDrag storage unit
  6899.         SUFocusAndSetValue(ev, testDragSU, kDragTextPropText, kODAppleTEXT, (ODValue) textPtr, textSize);
  6900. #endif
  6901.  
  6902.         HSetState(textHandle, textState);
  6903.  
  6904.         if ( textSize > 0 )
  6905.         {
  6906.             short saveStart = (**(_fDocument->theTE)).selStart;
  6907.             short saveEnd   = (**(_fDocument->theTE)).selEnd;
  6908.             TESetSelect(0, 32767, _fDocument->theTE);
  6909.  
  6910.             stylHandle = TEGetStyleScrapHandle(_fDocument->theTE);
  6911.             ODULong stylSize = ODGetHandleSize((ODHandle) stylHandle);
  6912.             void* stylPtr = ODLockHandle((ODHandle) stylHandle);
  6913.  
  6914.             // Write the styl information to the content property
  6915.             SUFocusAndSetValue(ev, storageUnit, kODPropContents, kODApplestyl, (ODValue) stylPtr, stylSize);
  6916.  
  6917. #ifdef SUPPORT_TESTDRAG_KIND
  6918.             // …and to the TestDrag storage unit
  6919.             SUFocusAndSetValue(ev, testDragSU, kDragTextPropStyl, kODApplestyl, (ODValue) stylPtr, stylSize);
  6920. #endif
  6921.  
  6922.             ODUnlockHandle((ODHandle)stylHandle);
  6923.             ODDisposeHandle((Handle)stylHandle);
  6924.  
  6925.             TESetSelect(saveStart, saveEnd, _fDocument->theTE);
  6926.         }
  6927.  
  6928. #ifdef SUPPORT_TESTDRAG_KIND
  6929.         //
  6930.         // Write out the source and destination links
  6931.         //
  6932.         if (_fSrcLink.linkSource != (ODLinkSource*) kODNULL)
  6933.         {
  6934.             clonedID = _fDraft->Clone(ev, key, _fSrcLink.linkSource->GetID(ev), 0, scopeFrameID);
  6935.             PRINT("Cloned linkSource ID = %d to ID = %d\n", _fSrcLink.linkSource->GetID(ev), clonedID);
  6936.  
  6937.             if ( clonedID != kODNULLID )
  6938.             {
  6939.                 SUForceFocus(ev, testDragSU, kDragTextPropSourceLink, kDragTextValue);
  6940.                 testDragSU->GetStrongStorageUnitRef(ev, clonedID, aSURef);
  6941.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODStorageUnitRef), aSURef);
  6942.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &_fSrcLink.start);
  6943.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &_fSrcLink.end);
  6944.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODULong), (ODValue) &_fSrcLink.change);
  6945.             }
  6946.         }
  6947.     
  6948.         if (_fDstLink.link != (ODLink*) kODNULL)
  6949.         {
  6950.             clonedID = _fDraft->Clone(ev, key, _fDstLink.link->GetID(ev), 0, scopeFrameID);
  6951.             PRINT("Cloned link ID = %d to ID = %d\n", _fDstLink.link->GetID(ev), clonedID);
  6952.  
  6953.             if ( clonedID != kODNULLID )
  6954.             {
  6955.                 SUForceFocus(ev, testDragSU, kDragTextPropDestLink, kDragTextValue);
  6956.                 testDragSU->GetStrongStorageUnitRef(ev, clonedID, aSURef);
  6957.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODStorageUnitRef), aSURef);
  6958.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &_fDstLink.start);
  6959.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODSShort), (ODValue) &_fDstLink.end);
  6960.         
  6961.                 ODType kind = _fDstLink.info.kind;
  6962.                 _fDstLink.info.kind = (ODType) ODISOStrLength(kind) + 1;
  6963.                 StorageUnitSetValue(testDragSU, ev, sizeof(ODLinkInfo), (ODValue) &_fDstLink.info);
  6964.                 StorageUnitSetValue(testDragSU, ev, (ODULong) _fDstLink.info.kind, (ODValue) kind);
  6965.                 _fDstLink.info.kind = kind;
  6966.             }
  6967.         }
  6968.     
  6969.         SUForceFocus(ev, storageUnit, kODPropContents, kODKindTestDrag);
  6970.         storageUnit->GetStrongStorageUnitRef(ev, testDragSU->GetID(ev), aSURef);
  6971.         StorageUnitSetValue(storageUnit, ev, sizeof(ODStorageUnitRef), aSURef);
  6972. #endif
  6973.  
  6974. #ifdef SUPPORT_TESTDRAG_KIND
  6975.         ODReleaseObject(ev, testDragSU);
  6976. #endif
  6977.     }
  6978.  
  6979.     SOM_CATCH_ALL
  6980.     SOM_ENDTRY
  6981. }
  6982.  
  6983. SOM_Scope void  SOMLINK AppleTestDrag_DragTextExternalizeKinds(AppleTestDrag_DragText *somSelf, Environment *ev,
  6984.         ODTypeList* kindset)
  6985. {
  6986.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6987.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ExternalizeKinds");
  6988.  
  6989.     ODUnused(kindset);
  6990. }
  6991.  
  6992. SOM_Scope void  SOMLINK AppleTestDrag_DragTextChangeKind(AppleTestDrag_DragText *somSelf, Environment *ev,
  6993.         ODType kind)
  6994. {
  6995.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  6996.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","ChangeKind");
  6997.  
  6998.     ODUnused(kind);
  6999. }
  7000.  
  7001.  
  7002.  
  7003. SOM_Scope void  SOMLINK AppleTestDrag_DragTextInitPart(AppleTestDrag_DragText *somSelf, Environment *ev,
  7004.         ODStorageUnit* storageUnit, ODPart* partWrapper)
  7005. {
  7006.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  7007.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InitPart");
  7008.  
  7009.     SOM_TRY
  7010.  
  7011.     _fPartWrapper = partWrapper;
  7012.     
  7013.     parent_InitPart(somSelf, ev, storageUnit, partWrapper);
  7014.  
  7015.     somSelf->CommonInitDragText(ev, storageUnit);
  7016.  
  7017.     storageUnit->AddProperty(ev, kODPropContents);
  7018.     
  7019. #ifdef SUPPORT_TESTDRAG_KIND
  7020.     storageUnit->AddValue(ev, kODKindTestDrag);
  7021. #else
  7022.     storageUnit->AddValue(ev, kODAppleTEXT);
  7023. #endif
  7024.  
  7025.     _fDraft = storageUnit->GetDraft(ev);
  7026.  
  7027. #ifdef SUPPORT_TESTDRAG_KIND
  7028.     _fTestDragSU = _fDraft->CreateStorageUnit(ev);
  7029. #endif
  7030.  
  7031.     _fDirty = kODTrue;
  7032.     
  7033.     SOM_CATCH_ALL
  7034.     SOM_ENDTRY
  7035. }
  7036.  
  7037. SOM_Scope void  SOMLINK AppleTestDrag_DragTextInitPartFromStorage(AppleTestDrag_DragText *somSelf, Environment *ev,
  7038.         ODStorageUnit* storageUnit, ODPart* partWrapper)
  7039. {
  7040.     AppleTestDrag_DragTextData *somThis = AppleTestDrag_DragTextGetData(somSelf);
  7041.     AppleTestDrag_DragTextMethodDebug("AppleTestDrag_DragText","InitPartFromStorage");
  7042.  
  7043.     SOM_TRY
  7044.  
  7045.     _fPartWrapper = partWrapper;
  7046.     
  7047.     parent_InitPartFromStorage(somSelf, ev, storageUnit, partWrapper);
  7048.  
  7049.     somSelf->CommonInitDragText(ev, storageUnit);
  7050.  
  7051.     Size            textSize, stylSize;
  7052.     ODStorageUnit    *partSU;
  7053.  
  7054. #ifdef SUPPORT_TESTDRAG_KIND
  7055.      ODStorageUnitRef aSURef;
  7056. #endif
  7057.     
  7058. // $$$$$ OBSOLETE: Volatile(partSU);
  7059.     partSU = somSelf->GetStorageUnit(ev);
  7060.     _fDraft = partSU->GetDraft(ev);
  7061.  
  7062. #ifdef SUPPORT_TESTDRAG_KIND
  7063.     // $$$$$ This part should initialize from the first value kind that it supports,
  7064.     // $$$$$ not necessarily its native representation.
  7065.     // This editor's preference is for content of type kODKindTestDrag, so look for it first
  7066.     if ( partSU->Exists(ev, kODPropContents, kODKindTestDrag, 0) )
  7067.     {
  7068.         partSU->Focus(ev, kODPropContents,kODPosSame,kODKindTestDrag,1,kODPosFirstSib);
  7069.         StorageUnitGetValue(partSU, ev, sizeof(ODStorageUnitRef),&aSURef);
  7070.     
  7071.         ASSERT(partSU->IsValidStorageUnitRef(ev, aSURef), kODErrInvalidStorageUnitRef);
  7072.     
  7073.         _fTestDragSU = _fDraft->AcquireStorageUnit(ev, partSU->GetIDFromStorageUnitRef(ev, aSURef));
  7074.  
  7075.         // TEXT property won't exist in stationery
  7076.         if ( SUExistsThenFocus(ev, _fTestDragSU, kDragTextPropText, kODAppleTEXT) )
  7077.         {
  7078.             textSize = _fTestDragSU->GetSize(ev);
  7079.             if (textSize > 0)
  7080.             {
  7081.                 _fTextHandle = (Handle) ODNewHandle(textSize);
  7082.                 ODLockHandle((ODHandle) _fTextHandle);
  7083.                 StorageUnitGetValue(_fTestDragSU, ev, textSize, (ODValue)*_fTextHandle);
  7084.                 ODUnlockHandle((ODHandle) _fTextHandle);
  7085.             }
  7086.         }
  7087.     
  7088.         // styl property won't exist in stationery
  7089.         if ( SUExistsThenFocus(ev, _fTestDragSU, kDragTextPropStyl, kODApplestyl) )
  7090.         {
  7091.             stylSize = _fTestDragSU->GetSize(ev);
  7092.             if (stylSize > 0)
  7093.             {
  7094.                 _fStylHandle = (StScrpHandle)ODNewHandle(stylSize);
  7095.                 ODLockHandle((ODHandle) _fStylHandle);
  7096.                 StorageUnitGetValue(_fTestDragSU, ev, stylSize, (ODValue)*_fStylHandle);
  7097.                 ODUnlockHandle((ODHandle) _fStylHandle);
  7098.             }
  7099.         }
  7100.         
  7101.         //
  7102.         //  Read in the display frame; there can be only one but there may be none.
  7103.         //
  7104.  
  7105.         if ( SUExistsThenFocus(ev, _fTestDragSU, kODPropDisplayFrames, kODWeakStorageUnitRefs) )
  7106.         {
  7107.             if ( _fTestDragSU->GetSize(ev) > 0 )
  7108.             {
  7109.                 TRY
  7110.                 
  7111.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODStorageUnitRef), (ODValue)&aSURef);
  7112.                     if ( _fTestDragSU->IsValidStorageUnitRef(ev, aSURef) )
  7113.                     {
  7114.                         ODFrame* aFrame = _fDraft->AcquireFrame(ev, _fTestDragSU->GetIDFromStorageUnitRef(ev, aSURef));
  7115.                         _fDisplayFrames->AddLast((ElementType)aFrame);
  7116.                     }
  7117.                     
  7118.                 CATCH_ALL
  7119.                 
  7120.                 ENDTRY
  7121.             }
  7122.         }
  7123.     
  7124.         //
  7125.         // Read in the source and destination links
  7126.         //
  7127.         if ( SUExistsThenFocus(ev, _fTestDragSU, kDragTextPropSourceLink, kDragTextValue) )
  7128.         {
  7129.             ODBoolean validLink = kODTrue;
  7130.             TRY
  7131.                 StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODStorageUnitRef),&aSURef);
  7132.                 if ( _fTestDragSU->IsValidStorageUnitRef(ev, aSURef) )
  7133.                 {
  7134.                     _fSrcLink.linkSource = _fDraft->AcquireLinkSource(ev, _fTestDragSU->GetIDFromStorageUnitRef(ev, aSURef));
  7135.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fSrcLink.start);
  7136.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fSrcLink.end);
  7137.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODULong), (ODValue) &_fSrcLink.change);
  7138.  
  7139.                     // Ensure the link source references this part
  7140.                     _fSrcLink.linkSource->SetSourcePart(ev, somSelf->GetStorageUnit(ev));
  7141.                 }
  7142.                 else
  7143.                 {
  7144.                     validLink = kODFalse;
  7145.                 }
  7146.             CATCH_ALL
  7147.                 validLink = kODFalse;
  7148.             ENDTRY
  7149.  
  7150.             if ( !validLink )
  7151.             {
  7152.                 _fTestDragSU->SetOffset(ev, 0);
  7153.                 _fTestDragSU->DeleteValue(ev, _fTestDragSU->GetSize(ev));
  7154.                 _fSrcLink.linkSource = (ODLinkSource*) kODNULL;
  7155.                 _fSrcLink.start = -1;
  7156.                 _fSrcLink.end = -1;
  7157.                 _fSrcLink.change = kODUnknownUpdate;
  7158.             }
  7159.         }
  7160.     
  7161.         if ( SUExistsThenFocus(ev, _fTestDragSU, kDragTextPropDestLink, kDragTextValue) )
  7162.         {
  7163.             ODBoolean validLink = kODTrue;
  7164.             TRY
  7165.                 StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODStorageUnitRef),&aSURef);
  7166.                 if ( _fTestDragSU->IsValidStorageUnitRef(ev, aSURef) )
  7167.                 {
  7168.                     _fDstLink.link = _fDraft->AcquireLink(ev, _fTestDragSU->GetIDFromStorageUnitRef(ev, aSURef), (ODLinkSpec*) kODNULL);
  7169.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fDstLink.start);
  7170.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODSShort), (ODValue) &_fDstLink.end);
  7171.                     StorageUnitGetValue(_fTestDragSU, ev, sizeof(ODLinkInfo), (ODValue) &_fDstLink.info);
  7172.                     ODType kind = (ODType) ODNewPtr((ODULong) _fDstLink.info.kind, kDefaultHeapID);
  7173.                     StorageUnitGetValue(_fTestDragSU, ev, (ODULong) _fDstLink.info.kind, (ODValue) kind);
  7174.                     _fDstLink.info.kind = kind;
  7175.                 }
  7176.                 else
  7177.                 {
  7178.                     validLink = kODFalse;
  7179.                 }
  7180.             CATCH_ALL
  7181.                 validLink = kODFalse;
  7182.             ENDTRY
  7183.  
  7184.             if ( !validLink )
  7185.             {
  7186.                 _fTestDragSU->SetOffset(ev, 0);
  7187.                 _fTestDragSU->DeleteValue(ev, _fTestDragSU->GetSize(ev));
  7188.                 _fDstLink.link = (ODLink*) kODNULL;
  7189.                 _fDstLink.start = -1;
  7190.                 _fDstLink.end = -1;
  7191.             }
  7192.         }
  7193.     }
  7194.     else 
  7195. #endif
  7196.  
  7197.     if ( SUExistsThenFocus(ev, partSU, kODPropContents, kODApplestxt) )
  7198.     {
  7199.         Handle            textHandle;
  7200.         StScrpHandle    stylHandle;
  7201.         
  7202.         ReadStyledText(ev, partSU, &textHandle, &stylHandle);
  7203.         _fTextHandle = textHandle;
  7204.         _fStylHandle = stylHandle;
  7205.     }
  7206.     else if ( partSU->Exists(ev, kODPropContents, kODAppleTEXT, 0) )
  7207.     {
  7208.         // read in TEXT
  7209.         partSU->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  7210.         textSize = partSU->GetSize(ev);
  7211.         if (textSize > 0)
  7212.         {
  7213.             _fTextHandle = (Handle) ODNewHandle(textSize);
  7214.             ODLockHandle((ODHandle) _fTextHandle);
  7215.             StorageUnitGetValue(partSU, ev, textSize, (ODValue)*_fTextHandle);
  7216.             ODUnlockHandle((ODHandle) _fTextHandle);
  7217.         }
  7218.  
  7219.         // read in styl
  7220.         if ( SUExistsThenFocus(ev, partSU, kODPropContents, kODApplestyl) )
  7221.         {
  7222.             stylSize = partSU->GetSize(ev);
  7223.             if (stylSize > 0)
  7224.             {
  7225.                 _fStylHandle = (StScrpHandle)ODNewHandle(stylSize);
  7226.                 ODLockHandle((ODHandle) _fStylHandle);
  7227.                 StorageUnitGetValue(partSU, ev, stylSize, (ODValue)*_fStylHandle);
  7228.                 ODUnlockHandle((ODHandle) _fStylHandle);
  7229.             }
  7230.         }
  7231.     }
  7232.     else if ( partSU->Exists(ev, kODPropContents, kODAppleFileTEXT, 0) )
  7233.     {
  7234.         PRINT("DragText: Initializing from TEXT file\n");
  7235.         FSSpec fileSpec;
  7236.         if ( GetFSSpecFromHFSProperty(ev, partSU, &fileSpec) )
  7237.         {
  7238.             ODBoolean truncated;
  7239.             PRINT("DragText: Reading TEXT file\n");
  7240.             TRY
  7241.                 _fTextHandle = ReadTextFile(&fileSpec, &truncated);
  7242.             CATCH_ALL
  7243.                 // Put up an alert; unfortunately this part has no frame yet
  7244.                 RERAISE;
  7245.             ENDTRY;
  7246.             
  7247.             if ( &truncated )
  7248.             {
  7249.                 // Put up an alert; unfortunately this part has no frame yet
  7250.             }
  7251.             
  7252. //            SURemoveValue(ev, partSU, kODPropContents, kODAppleFileTEXT);    // $$$$$ Right thing to do?
  7253. //            SURemoveValue(ev, partSU, kODPropContents, kODApplehfs);
  7254.             SUForceFocus(ev, partSU, kODPropContents, kODAppleTEXT);
  7255.         }
  7256.         else
  7257.             THROW(kDragTextErrCantReadTEXTFile);
  7258.     }
  7259.     else
  7260.     {
  7261.         // No content type that this editor handles was found.
  7262.         // This editor should not have been bound to the part.
  7263.         // Throw an error.
  7264.         THROW(kDragTextErrNoSupportedContent);
  7265.     }
  7266.  
  7267.     SOM_CATCH_ALL
  7268.     SOM_ENDTRY
  7269. }
  7270.